1. 程式人生 > >Java釋出一個簡單 webservice應用 併發送SOAP請求

Java釋出一個簡單 webservice應用 併發送SOAP請求

  <?xml version="1.0" encoding="UTF-8" ?> 
- <!--  Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.6b21  svn-revision#12959. 
  --> 
- <!--  Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.6b21  svn-revision#12959. 
  -->
- <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("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            sb.append("<soap:Envelope "
                    + "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("<soap:Body>");
            sb.append("<api:getModifyRecords>");
            sb.append("<arg0>ls</arg0>");
            sb.append("</api:getModifyRecords>");
            sb.append("</soap:Body>");
            sb.append("</soap:Envelope>");


            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());
    }

響應報文如下:

<?xml version="1.0" ?>
  <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方式擷取