HTTPClient作为客户端调用webservice推送soap

最近对接了几个运营商的接口,整的也是焦头烂额的,好歹是完成了。

先记录一下这个,测试的话直接soapUI更方便,其他的后续整理整理再慢慢发布吧 。


public static void main(String args[]) throws Exception {
  
    String url = "http://ip:port/url?wsdl";
    StringBuilder soapBuilder = new StringBuilder(64);
    soapBuilder.append("");

    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(url);
    RequestConfig config = RequestConfig.custom().setConnectTimeout(1000) // 创建连接的最长时间
            .setConnectionRequestTimeout(500) // 从连接池中获取到连接的最长时间
            .setSocketTimeout(3 * 1000) // 数据传输的最长时间10s
            .build();
    httpPost.setConfig(config);
    CloseableHttpResponse response = null;
    try {
        //采用SOAP1.1调用服务端,这种方式能调用服务端为soap1.1和soap1.2的服务
        httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");

        //采用SOAP1.2调用服务端,这种方式只能调用服务端为soap1.2的服务
        // httpPost.setHeader("Content-Type", "application/soap+xml;charset=UTF-8");
        StringEntity stringEntity = new StringEntity(soapBuilder.toString(), Charset.forName("UTF-8"));
        httpPost.setEntity(stringEntity);
        response = httpClient.execute(httpPost);

        // 200为成功
        if (response.getStatusLine().getStatusCode() == 200) {
            String content = EntityUtils.toString(response.getEntity(), "UTF-8");
            //返回报文内容
            System.out.println(content);
        } else {
            System.out.println("调用失败!");
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (null != response) {
            response.close();
        }
        if (null != httpClient) {
            httpClient.close();
        }
    }


}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值