Server returned HTTP response code: 500 for URL解决方法

最近在做web service相关的东西,老师的作业题如下:

开发一个Java类,手工编写WSDL文档,并打包发布到Axis容器;客户端调用程序:基于Socket或HTTP库,自己构造SOAP调用报文,发送给服务器端,接收到响应的SOAP报文后,显示出来。

在编写客户端的时候,使用了http和socket两种方法,我用的是jre自有的api,http方式代码如下:

			URL url = new URL("http://localhost:8080/WebServiceTest2/services/Add");
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setRequestMethod("POST");
			connection.setDoInput(true);
            connection.setDoOutput(true);       
            connection.setRequestProperty("Pragma:", "no-cache");       
            connection.setRequestProperty("Cache-Control", "no-cache");       
            connection.setRequestProperty("Content-Type", "text/xml");
            connection.setRequestProperty("ContentType","text/xml;charset=utf-8"); 
            connection.setRequestProperty("charset", "UTF-8"); 
            connection.setRequestProperty("Accept-Language", "en-us,en;q=0.5");
            connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT //5.1)AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.46 Safari/535.11");
			
			OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
			String soapInfo = getSoapInfo();
			System.out.println(soapInfo);
			out.write(new String(soapInfo));
			out.flush();
			out.close();
			
			BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			String responseLine = "";
			StringBuffer stringBuffer = new StringBuffer();
			while((responseLine = reader.readLine()) != null) {
				stringBuffer.append(new String(responseLine.getBytes(), "UTF-8"));
			}
			System.out.println(stringBuffer.toString());

遇到了java.io.IOException: Server returned HTTP response code: 500 for URL错误信息,网上找了很多,例如修改User-Agent等都没能解决,我感觉还是java 客户端的访问被拒绝了,

最后使用了org.apache.commons.httpclient.HttpClient包,通信代码如下:

				String url = "http://localhost:8080/WebServiceTest2/services/Add";

				byte[] requestBytes;
				String soapRequestInfo = addClient.getSoapRequestInfo();
				requestBytes = soapRequestInfo.getBytes("utf-8");

				HttpClient httpClient = new HttpClient();
				PostMethod postMethod = new PostMethod(url);
				postMethod.setRequestHeader("SOAPAction", "http://tempuri.org/GetMiscInfo");//Soap Action Header!

				InputStream inputStream = new ByteArrayInputStream(requestBytes, 0, requestBytes.length);
				RequestEntity requestEntity = new InputStreamRequestEntity(inputStream, requestBytes.length, "application/soap+xml; charset=utf-8");
				postMethod.setRequestEntity(requestEntity);

				int state = httpClient.executeMethod(postMethod);

				InputStream soapResponseStream = postMethod.getResponseBodyAsStream();
				InputStreamReader inputStreamReader = new InputStreamReader(soapResponseStream);
				BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
			
				String responseLine = "";
				String soapResponseInfo = "";
				while((responseLine = bufferedReader.readLine()) != null) {
					soapResponseInfo = soapResponseInfo + responseLine;
				}

使用apache的HttpClient最后实现了与服务器端web service的通信,不得不说apache的HttpClient包做的强大啊。


你可能感兴趣的:(Web,Service,web)