Server returned HTTP response code: 500 for URL: http.......错误

在前不久做一个项目需要调用第三方接口,但一直报Server returned HTTP response code: 500 for URL: http.......错误,经过调试接口可正常调用并返回结果,如下是我改之前与之后的代码:

之前:

URL wsUrl2 = new URL(wsUrl);
				HttpURLConnection conn = (HttpURLConnection) wsUrl2.openConnection();
				conn.setDoInput(true);
				conn.setDoOutput(true);
				conn.setRequestMethod("POST");
				conn.setConnectTimeout(5*1000);
				conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
				conn.setRequestProperty("SOAPAction",wsUrl);
				
				OutputStream os = conn.getOutputStream();
				os.write(xml.getBytes());
				System.out.println("conn.getInputStream()===="+conn);
				InputStream is = conn.getInputStream();
				byte[] b = new byte[1024];
				int len = 0;
				String s = "";
				while((len = is.read(b)) != -1){
					String ss = new String(b,0,len,"UTF-8");
					s += ss;
				}
				is.close();
				os.close();
				conn.disconnect();

修改之后:

URL wsUrl2 = new URL(wsUrl);
				HttpURLConnection conn = (HttpURLConnection) wsUrl2.openConnection();
				conn.setDoInput(true);
				conn.setDoOutput(true);
				conn.setRequestMethod("POST");
				conn.setConnectTimeout(5*1000);
				conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
				conn.setRequestProperty("SOAPAction",wsUrl);
				
				OutputStream os = conn.getOutputStream();
				os.write(xml.getBytes("UTF-8"));
				System.out.println("conn.getInputStream()===="+conn);
				InputStream is = conn.getInputStream();
				byte[] b = new byte[1024];
				int len = 0;
				String s = "";
				while((len = is.read(b)) != -1){
					String ss = new String(b,0,len,"UTF-8");
					s += ss;
				}
				is.close();
				os.close();
				conn.disconnect();


os.write(xml.getBytes("UTF-8"));  为我更改的内容。

原因分析:因为我调用接口时所传输的参数有中文,所以在调用的过程中内容变为乱码,不能返回数据。更改后乱码问题即可解决。

你可能感兴趣的:(java,随笔)