牧者

大风起兮云飞扬

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

使用HttpClient对Web Service调用

转http://www.blogjava.net/mrcold/archive/2010/05/27/322051.html

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

2具体流程大致这样:

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请求数据添加到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对象,并发出postMethod请求

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


3动态构造调用串的话如下实现:


  public String tns="needtel.rmp.portal";//命名空间
  public String methodName="BeginSession";//方法名
  public String wsdlLocation="http://127.0.0.1/RMPPortal/Service.asmx?wsdl";//地址
  public 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) {
  //构建请求XML格式参数

  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 2013-02-20 13:14  牧者.D  阅读(320)  评论(0)    收藏  举报