httpclent调用webservice

httpclent调用 webservice   wsdl后缀服务

1.jar包:

commons-logging-1.2.jar

commons.codec_1.3.jar

commons.httpclient_3.1.jar

dom4j-1.4.jar

httpcore_4.0-beta1.jar

2.地址:格式如【http://192.168.123.89:7031/cus/EmvsWs?wsdl

3.soap类型的xml字符串

用soapui生成,右侧的就是

 Center

红框内的部分就是soap类型的xml字符串,拷贝到代码中,当然要将你的参数替换到 ?用拼接

4. 请求和传输时间自己随意设置。

5. soapAction就不用管,直接“”

6. 返回值就是一串字符串,类似上图中的红框下边那一部分,将它转换成document。再取得其中的值就可以了

 

/**

postUrl:远程地址

soapXml:soap类型的xml字符串

soapAction:默认””

socketTimeout:请求超时时间

connectTimeout:传输超时时间

*/

 public static StringdoPostSoap1_1(StringpostUrl, StringsoapXml, String soapAction, StringsocketTimeout, StringconnectTimeout) {  

    String retStr = ""; 

    //HttpClient

        HttpClient httpClient =new HttpClient();

        //PostMethod

        PostMethod postMethod =new PostMethod(postUrl);

        //  设置请求和传输超时时间  

        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(Integer.parseInt(socketTimeout));//连接超时

        httpClient.getHttpConnectionManager().getParams().setSoTimeout(Integer.parseInt(connectTimeout));

        //设置请求体

        try {

            RequestEntity requestEntity = new ByteArrayRequestEntity(soapXml.getBytes("utf-8"));  

            postMethod.setRequestEntity(requestEntity);

            //postMethod.setRequestBody(soapXml);  //方法过时

            //设置请求参数

            postMethod.setRequestHeader("Content-Type", "text/xml;charset=UTF-8");

            postMethod.setRequestHeader("SOAPAction", soapAction);

            httpClient.executeMethod(postMethod);  //发送请求

            retStr=postMethod.getResponseBodyAsString();//响应体

            System.out.println("retStr:"+retStr);

            //将相应体转换成document解析,并取得其中的数据

            retStr =DocumentHelper.parseText(retStr).getRootElement().element("Body").element("uploadCusxDownUpInfoResponse").element("result").getText();

        } catch (Exceptione) {

            e.printStackTrace();

        }finally{

        //关闭链接

            if(postMethod!= null)
            {
                postMethod.releaseConnection(); 

            }
        }
        System.out.println("retStr:"+retStr);

        return retStr;

    }  

 

 

 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

幻凡ss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值