根据HttpClient调用Soap服务端

1.导入jia包

同步请求所用到的包是:

<dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.2</version>
    </dependency>

异步请求用到的包是:

<dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpasyncclient</artifactId>
        <version>4.1.2</version>

</dependency>

1.xml

SSOAP 1.1

The following is a sample SOAP 1.1 request and response. The placeholders shown need to be replaced with actual values.

POST /ServiceApi.asmx HTTP/1.1
Host: 0.0.0.0
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://www.uradiosys.com/HostAddOrUpdateByOtherSys"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <HostAddOrUpdateByOtherSys xmlns=
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用 HttpClient 发送 SOAP 请求时,需要在请求头中添加一个 SoapAction 头部,指定所调用SOAP 方法。可以使用 HttpClient 的 setHeader 方法来添加 SoapAction 头部,示例代码如下: ```java import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.message.BasicHeader; import org.apache.http.protocol.HTTP; public class SoapClient { public static void main(String[] args) throws Exception { String url = "http://example.com/soap"; String soapAction = "http://example.com/soap/HelloWorld"; // 创建 HttpClient 实例 HttpClient httpClient = HttpClientBuilder.create().build(); // 创建 HttpPost 请求 HttpPost httpPost = new HttpPost(url); // 设置 SoapAction 头部 httpPost.setHeader(new BasicHeader("SOAPAction", soapAction)); // 设置请求体 String requestBody = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" + " <soap:Body>\n" + " <HelloWorld xmlns=\"http://example.com/soap\">\n" + " <name>World</name>\n" + " </HelloWorld>\n" + " </soap:Body>\n" + "</soap:Envelope>"; HttpEntity requestEntity = new StringEntity(requestBody, HTTP.UTF_8); httpPost.setEntity(requestEntity); // 发送请求并获取响应 HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity responseEntity = httpResponse.getEntity(); String responseBody = EntityUtils.toString(responseEntity, HTTP.UTF_8); // 打印响应内容 System.out.println(responseBody); } } ``` 在上面的代码中,我们使用了 setHeader 方法来设置 SoapAction 头部,其参数为一个 BasicHeader 对象,其构造函数接收两个参数:头部名称和头部值。在发送请求时,我们将 HttpPost 对象传递给 HttpClient 的 execute 方法,并获取响应的实体内容。最后,我们把响应实体转换成字符串并打印出来。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值