httpclient 调用webservice

本文详细介绍了如何使用HttpClient库对WebService进行调用,包括所需jar包、具体流程及动态构造调用串的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

使用HttpClient对Web Service调用

用到的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请求数据添加到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); 


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

 

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


 

HttpClient是一个开源的HTTP客户端库,用发送HTTP请求和接收HTTP响应。它提供了一种简单而灵活的方式来与Web服务进行通信。 使用HttpClient调用WebService可以分为以下几个步骤: 1. 创建HttpClient对象:首先需要创建一个HttpClient对象,用于发送HTTP请求和接收HTTP响应。 2. 创建请求对象:根据WebService的要求,创建一个HttpRequest对象,设置请求的URL、请求方法(GET、POST等)、请求头、请求体等信息。 3. 发送请求:使用HttpClient对象发送请求,并获取到HttpResponse对象作为响应。 4. 处理响应:从HttpResponse对象中获取响应状态码、响应头、响应体等信息,并根据需要进行处理。 5. 关闭连接:使用完毕后,需要关闭HttpClient对象和HttpResponse对象,释放资源。 下面是一个示例代码: ```java import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; public class WebServiceClient { public static void main(String[] args) { HttpClient httpClient = HttpClientBuilder.create().build(); HttpGet httpGet = new HttpGet("http://example.com/webservice"); try { HttpResponse response = httpClient.execute(httpGet); int statusCode = response.getStatusLine().getStatusCode(); HttpEntity entity = response.getEntity(); String responseBody = EntityUtils.toString(entity); // 处理响应 System.out.println("Status Code: " + statusCode); System.out.println("Response Body: " + responseBody); // 关闭连接 httpClient.close(); } catch (Exception e) { e.printStackTrace(); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值