java使用POST发送soap报文请求webservice返回500错误解析

文章摘要:

本文使用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
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值