一路拾遺
          Collect By Finding All The Way ......
          posts - 81,comments - 41,trackbacks - 0

          用到的jar包有:
              commons-codec-1.3.jar
              commons-httpclient-3.0.jar
              commons-logging-1.0.4.jar


          具體流程大致這樣:

                  
                  String soapRequestData 
          = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                  
          "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">" +
                    "<soap12:Body>" +
                     
          " <qqCheckOnline xmlns=\"http://WebXml.com.cn/\">" +
                    "    <qqCode>349104641</qqCode>" +
                   
          "   </qqCheckOnline>" +
                  
          "  </soap12:Body>" +
                  
          "</soap12:Envelope>";
                  
                  System.out.println(soapRequestData);


              
          //PostMethod postMethod = new PostMethod("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl");
              
              PostMethod postMethod 
          = new PostMethod("http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl");

             
          // 然后把Soap請求數(shù)據(jù)添加到PostMethod中


              
          byte[] b = soapRequestData.getBytes("utf-8");
              InputStream is 
          = new ByteArrayInputStream(b,0,b.length);
              RequestEntity re 
          = new InputStreamRequestEntity(is,b.length,"application/soap+xml; charset=utf-8");
              postMethod.setRequestEntity(re); 
              
              
          //最后生成一個HttpClient對象,并發(fā)出postMethod請求

              HttpClient httpClient 
          = new HttpClient();
              
          int statusCode = httpClient.executeMethod(postMethod);
              String soapResponseData 
          =  postMethod.getResponseBodyAsString();
                  
                  
              System.out.print(soapResponseData);



          動態(tài)構造調(diào)用串的話如下實現(xiàn):


              
          private String tns;
              
          private String methodName;
              
          private String wsdlLocation;
              
          private String soapResponseData;

          private int invoke(Map<String, String> patameterMap) throws Exception {
                  PostMethod postMethod 
          = new PostMethod(wsdlLocation);
                  String soapRequestData 
          = buildRequestData(patameterMap);

                  
          byte[] bytes = soapRequestData.getBytes("utf-8");
                  InputStream inputStream 
          = new ByteArrayInputStream(bytes, 0,
                          bytes.length);
                  RequestEntity requestEntity 
          = new InputStreamRequestEntity(inputStream,
                          bytes.length, 
          "application/soap+xml; charset=utf-8");
                  postMethod.setRequestEntity(requestEntity);

                  HttpClient httpClient 
          = new HttpClient();
                  
          int statusCode = httpClient.executeMethod(postMethod);
                  soapResponseData 
          = postMethod.getResponseBodyAsString();
                  
          return statusCode;
              }


              
          private String buildRequestData(Map<String, String> patameterMap) {
                  StringBuffer soapRequestData 
          = new StringBuffer();
                  soapRequestData.append(
          "<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                  soapRequestData
                          .append(
          "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">");
                  soapRequestData.append("<soap12:Body>");
                  soapRequestData.append(
          "<" + methodName + " xmlns=\"" + tns + "\">");
                  Set
          <String> nameSet = patameterMap.keySet();
                  
          for (String name : nameSet) {
                      soapRequestData.append(
          "<" + name + ">" + patameterMap.get(name)
                              
          + "</" + name + ">");
                  }

                  soapRequestData.append(
          "</" + methodName + ">");
                  soapRequestData.append(
          "</soap12:Body>");
                  soapRequestData.append(
          "</soap12:Envelope>");

                  
          return soapRequestData.toString();
              }
          posted on 2010-05-27 16:30 胖胖泡泡 閱讀(1219) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 凤翔县| 保康县| 乐安县| 金山区| 陆川县| 仪征市| 沂源县| 阳新县| 濮阳县| 锡林郭勒盟| 景洪市| 明溪县| 当涂县| 杭州市| 玛纳斯县| 武鸣县| 垦利县| 杨浦区| 朝阳区| 宾阳县| 汽车| 合江县| 石家庄市| 璧山县| 白水县| 廊坊市| 通榆县| 苍山县| 汕尾市| 同心县| 江达县| 呼图壁县| 天祝| 霍林郭勒市| 闵行区| 新源县| 荔波县| 汶上县| 磐安县| 乌苏市| 天等县|