httpclient 调用wsdl
时间: 2023-10-06 12:10:46 浏览: 44
要使用 HttpClient 调用 WSDL,需要按照以下步骤进行操作:
1. 创建一个 HttpClient 对象。
2. 创建一个 HttpPost 对象,并设置请求的 URL。
3. 设置请求的 SOAPAction。
4. 构造 SOAP 请求的 XML,并将其设置为请求的实体。
5. 执行请求,并获取响应的实体。
6. 将响应的实体转换为字符串,并解析出所需的数据。
下面是一个示例代码:
```java
import org.apache.http.HttpEntity;
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.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class WsdlClient {
public static void main(String[] args) throws Exception {
String url = "http://example.com/MyService?wsdl";
String soapAction = "http://example.com/MyService/MyMethod";
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("SOAPAction", soapAction);
String soapXml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://example.com/MyService/\"><soapenv:Header/><soapenv:Body><ser:MyMethod><arg0>test</arg0></ser:MyMethod></soapenv:Body></soapenv:Envelope>";
StringEntity entity = new StringEntity(soapXml, "UTF-8");
httpPost.setEntity(entity);
HttpEntity responseEntity = httpClient.execute(httpPost).getEntity();
String responseXml = EntityUtils.toString(responseEntity, "UTF-8");
// 解析响应的 XML,并获取所需的数据
}
}
```
注意,这里使用的是 Apache HttpComponents,如果你使用的是其他的 HTTP 客户端库,具体的实现方式可能会有所不同。