Sealyu

          --- 博客已遷移至: http://www.sealyu.com/blog

            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            618 隨筆 :: 87 文章 :: 225 評(píng)論 :: 0 Trackbacks

          用到的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請(qǐng)求數(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); 
              
              
          //最后生成一個(gè)HttpClient對(duì)象,并發(fā)出postMethod請(qǐng)求

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



          動(dòng)態(tài)構(gòu)造調(diào)用串的話(huà)如下實(shí)現(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-07-27 14:42 seal 閱讀(1966) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): web
          主站蜘蛛池模板: 安宁市| 普宁市| 哈密市| 临沂市| 尼玛县| 新沂市| 阳高县| 剑川县| 邢台市| 青阳县| 巫溪县| 延川县| 库尔勒市| 托克托县| 邢台市| 九龙城区| 伊宁县| 阿瓦提县| 巴中市| 株洲市| 桂阳县| 新民市| 海晏县| 长垣县| 三江| 南平市| 久治县| 安图县| 桑植县| 镇原县| 淄博市| 英德市| 会宁县| 肥城市| 遂溪县| 邵阳市| 巴青县| 温泉县| 南投县| 拜城县| 长武县|