Java发布一个简单 webservice应用 并发送SOAP请求

首先搭建一个webservice的服务器,搭建成功后,运行项目,并访问 http://localhost:8080/service/SdicDataPort?wsdl“,得到如下wsdl文件,说明webservice发布成功:
Java发布一个简单 webservice应用 并发送SOAP请求_第1张图片

   
-  
-  
- <definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://sdic/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://sdic/" name="SdicDataService">
- <types>
- <xsd:schema>
  <xsd:import namespace="http://sdic/" schemaLocation="http://10.3.46.235:8080/service/SdicDataPort?xsd=1" /> 
  xsd:schema>
  types>
- <message name="getModifyRecords">
  <part name="parameters" element="tns:getModifyRecords" /> 
  message>
- <message name="getModifyRecordsResponse">
  <part name="parameters" element="tns:getModifyRecordsResponse" /> 
  message>
- <portType name="SdicDataDelegate">
- <operation name="getModifyRecords">
  <input wsam:Action="http://sdic/SdicDataDelegate/getModifyRecordsRequest" message="tns:getModifyRecords" /> 
  <output wsam:Action="http://sdic/SdicDataDelegate/getModifyRecordsResponse" message="tns:getModifyRecordsResponse" /> 
  operation>
  portType>
- <binding name="SdicDataPortBinding" type="tns:SdicDataDelegate">
  <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /> 
- <operation name="getModifyRecords">
  <soap:operation soapAction="" /> 
- <input>
  <soap:body use="literal" /> 
  input>
- <output>
  <soap:body use="literal" /> 
  output>
  operation>
  binding>
- <service name="SdicDataService">
- <port name="SdicDataPort" binding="tns:SdicDataPortBinding">
  <soap:address location="http://10.3.46.235:8080/service/SdicDataPort" /> 
  port>
  service>
  definitions>

客户端访问webservice
通过 HttpClient 及 HttpURLConnection 发送SOAP请求,代码如下:

public String remoteGetDate(String endpoint,String starttime,String endtime) throws HttpException, IOException {
            int timeout = 60000;
            StringBuffer sb = new StringBuffer("");


            sb.append("");
            sb.append("
                    + "xmlns:api='http://sdic/' "
                    + "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
                    + "xmlns:xsd='http://www.w3.org/2001/XMLSchema' "
                    + "xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>");
            sb.append("");
            sb.append("");
            sb.append("ls");
            sb.append("");
            sb.append("");
            sb.append("");


            try{
            // HttpClient发送SOAP请求
            System.out.println("HttpClient 发送SOAP请求");
            System.out.println("参数输出:"+sb.toString());
            HttpClient client = new HttpClient();
          //设置用户名密码
            //client.getState().setCredentials(AuthScope.ANY,new UsernamePasswordCredentials("SDICEAMUSER", "SDICEAMPASSWORD")); 
            PostMethod postMethod = new PostMethod(endpoint);
            // 设置连接超时
            client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);
            // 设置读取时间超时
            client.getHttpConnectionManager().getParams().setSoTimeout(timeout);
            // 然后把Soap请求数据添加到PostMethod中
            RequestEntity requestEntity = new StringRequestEntity(sb.toString(), "text/xml", "UTF-8");
            //设置请求头部,否则可能会报 “no SOAPAction header” 的错误
            postMethod.setRequestHeader("SOAPAction","");
            // 设置请求体
            postMethod.setRequestEntity(requestEntity);
            int status = client.executeMethod(postMethod);
            // 打印请求状态码
            System.out.println("status:" + status);
            // 获取响应体输入流
            InputStream is = postMethod.getResponseBodyAsStream();
            // 获取请求结果字符串
            String result = IOUtils.toString(is);
            System.out.println("result: " + result);  
            return result;
         }catch(ServiceLayerException se){
             throw new ServiceLayerException("SOAP请求发送失败!");
         }
    }


 // HttpURLConnection 发送SOAP请求(这个是转载的,需要修改下)
        System.out.println("HttpURLConnection 发送SOAP请求");
        URL url = new URL(wsdl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
        conn.setRequestMethod("POST");
        conn.setUseCaches(false);
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setConnectTimeout(timeout);
        conn.setReadTimeout(timeout);

        DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
        dos.write(sb.toString().getBytes("utf-8"));
        dos.flush();


        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
        String line = null;
        StringBuffer strBuf = new StringBuffer();
        while ((line = reader.readLine()) != null) {
            strBuf.append(line);
        }
        dos.close();
        reader.close();

        System.out.println(strBuf.toString());
    }

响应报文如下:


  <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body>
      <ns2:sayHelloResponse xmlns:ns2="http://demo.ls.com/">
        <return>************return>
      ns2:sayHelloResponse>
    S:Body>
  S:Envelope>

具体的数据是之中的内容,需要通过xml方式截取

你可能感兴趣的:(Web服务器)