文章摘要:
本文使用JAX-WS2.2编译webservice,并使用HttpUrlConnection的POST方式对wsdl发送soap报文进行请求返回数据,
对错误Server returned HTTP response code: 500 的解决方法进行简单分析。
问题描述:
由于课程需要博主需要自己写一个webservice并且通过soap进行请求,
于是使用JAX-WS编译了下面java代码生成webservice服务
生成webservice的java代码:
@WebService()
public class HelloWorld {
@WebMethod
public String sayHelloWorldFrom(String from) {
System.out.println("getMessage.");
String result = "Hello, world, from " + from;
System.out.println(result);
return result;
}
public static void main(String[] argv) {
System.out.println("Service is running...");
Object implementor = new HelloWorld ();
String address = "http://localhost:9000/HelloWorld";
Endpoint.publish(address, implementor);
}
}
查看webservice
在网上查到的一个方法就是通过HttpUrlConnection进行请求,这边贴一下代码,应该很多人都有查到类似的方法
HttpUrlConnection请求实现代码:
public static void main(String[] args) throws Exception
{
String urlString = "http://localhost:9000/HelloWorld?wsdl";//自定义的wsdl服务
URL url = new URL(urlString);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();//打开连接
String xmlFile = "soap_xml\\soap.xml";//要发送的soap格式文件
File fileToSend = new File(xmlFile);
byte[] buf = new byte[(int) fileToSend.length()];// 用于存放文件数据的数组
new FileInputStream(xmlFile).read(buf);
//Content-Length长度会自动进行计算
httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
httpConn.setRequestMethod("POST");
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
OutputStream out = httpConn.getOutputStream();
out.writ