httpClient访问webservice服务--带http头参数

httpClient访问webservice

之所以通过httpClient访问是因为有的webservice需要设置http header,比如利用soapUI工具时会设置http header,那么利用axis就没有办法设置http头(我没有找到),所以就需要利用httpClient进行webserivce访问,代码如下:
1, getData方法

private static String getData(String OperationCode, String param){
        String returnVal = "" ;
        StringBuilder xmlpara = new StringBuilder() ;
        xmlpara.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:user=\"http://127.0.0.1:8080/UserAction\">") ;
            xmlpara.append("<soapenv:Header/>") ;
            xmlpara.append("<soapenv:Body>") ; 
                xmlpara.append("<user:getUserDetailByName>") ;
                    xmlpara.append("<user:param>" + param + "</user:param>") ;
                xmlpara.append("</user:getUserDetailByName>") ;
            xmlpara.append("</soapenv:Body>") ; 
        xmlpara.append("</soapenv:Envelope>") ;

        String inputPara = xmlpara.toString() ;

        HashMap<String,String> res = new HashMap<>();
//          String endpoint = "http://124.205.248.2:8080/eSales/esales.asmx?WSDL";
            String endpoint = "http://192.168.0.123:9090/esb.user";
            PostMethod postMethod = new PostMethod(endpoint);
            byte[] b;
            try {
                b = inputPara.getBytes("utf-8");
                InputStream is = new ByteArrayInputStream(b,0,b.length);
                RequestEntity re = new InputStreamRequestEntity(is,b.length,"application/soap+xml; charset=utf-8");
                //把Soap请求数据添加到PostMethod中
                postMethod.setRequestEntity(re);
                postMethod.setRequestHeader("ClientId", "com.primeton.esb.hljsjzx.manage") ;
//              postMethod.setRequestHeader("OperationCode", "com.primeton.esb.hljsjzx.Infrastructure.user.getUserDetailByName") ;
                postMethod.setRequestHeader("OperationCode", OperationCode) ;
                //生成一个HttpClient对象,并发出postMethod请求
                HttpClient httpClient = new HttpClient();
                int statusCode = httpClient.executeMethod(postMethod);
                if(200==statusCode){
                    String getServerData =  postMethod.getResponseBodyAsString();
                    //System.out.println("----->"+getServerData);
                    //获取返回值状态标识,标识为0:成功;非0:失败
                    res.put("status", "0");
                    //res.put("msg", getServerData);
                    SAXReader sax = new SAXReader() ;

                    Document doc = sax.read(new ByteArrayInputStream(getServerData.getBytes("utf-8"))) ;
                    DefaultXPath xpath = new DefaultXPath("//ns1:out");
                    xpath.setNamespaceURIs(Collections.singletonMap("ns1","http://127.0.0.1:8080/UserAction")); 
                    Element ele = (Element)xpath.selectSingleNode(doc) ;
                    returnVal = ele.getText();                  
                }
            } catch (Exception e) {
                res.put("status", "1");
                res.put("msg", e.toString());
                e.printStackTrace();
            }
            return returnVal;
        }
  1. 方法调用
public static void main(String[] args) {   
        String operationCode = "com.primeton.esb.hljsjzx.Infrastructure.user.getUserDetailByName" ;
        Map map = getData(operationCode,xmlpara.toString()) ;
        String val = "" ;
        String status = (String)map.get("status");
        if(status.equals("0")){
            val = (String)map.get("msg") ;
        }
        SAXReader sax = new SAXReader() ;
        try {
            Document doc = sax.read(new ByteArrayInputStream(val.getBytes("utf-8"))) ;
            DefaultXPath xpath = new DefaultXPath("//ns1:out");
            xpath.setNamespaceURIs(Collections.singletonMap("ns1","http://127.0.0.1:8080/UserAction")); 
            Element ele = (Element)xpath.selectSingleNode(doc) ;
            System.out.println(ele.getText());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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、付费专栏及课程。

余额充值