HttpClient方式调用WebService接口

问题

因为使用axis1.4的webservice客户端老是调用失败,所以,想使用HttpClient客户端,直接使用http协议请求接口。

思路

使用soapui客户端请求webservice,通过soapui工具,就可以了解Http协议的样子,然后就可以通过HttpClient客户端工具,直接用Http协议请求webservice。

步骤

这里我们以一个天气预报的webservice作为例子:
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl

以wsdl创建soap项目:
根据wsdl创建项目
打开一个请求接口例子:
打开请求省份接口例子

使用soapui工具请求例子:
请求例子
从这里就可以看出来,webservice实际上就是http请求。更加这个http请求,我们就可以直接使用任何一款http工具进行请求了。

http实现

httpclient核心调用方法如下:

	static int socketTimeout = 30000;// 请求超时时间
	static int connectTimeout = 30000;// 传输超时时间
	public static String doPostSoap1_2(String postUrl, String soapXml,
									   String soapAction) throws Exception {
		String retStr = "";
		// 创建HttpClientBuilder
		HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
		// HttpClient
		CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
		HttpPost httpPost = new HttpPost(postUrl);
		// 设置请求和传输超时时间
		RequestConfig requestConfig = RequestConfig.custom()
				.setSocketTimeout(socketTimeout)
				.setConnectTimeout(connectTimeout).build();
		httpPost.setConfig(requestConfig);
		try {
			httpPost.setHeader("Content-Type",
					"application/soap+xml;charset=UTF-8");
			httpPost.setHeader("Cache-Control",
					"no-cache");
			httpPost.setHeader("Pragma",
					"no-cache");
			httpPost.setHeader("SOAPAction", soapAction);
			StringEntity data = new StringEntity(soapXml,
					StandardCharsets.UTF_8);
			httpPost.setEntity(data);
			CloseableHttpResponse response = closeableHttpClient
					.execute(httpPost);
			HttpEntity httpEntity = response.getEntity();
			if (httpEntity != null) {
				// 打印响应内容
				retStr = EntityUtils.toString(httpEntity, "UTF-8");
//				logger.info("response:" + retStr);
			}
			// 释放资源
			closeableHttpClient.close();
		} catch (Exception e) {
			throw e;
		} finally {
			if (closeableHttpClient != null){
				closeableHttpClient.close();
			}
		}
		return retStr;
	}

该方法的具体使用如下:

result = doPostSoap1_2(wsdlUri, soapXml, "");

当前这个例子,之前使用的axis1.4客户端,所以,这里在原来的基础进行修改:

import org.apache.axis.Message;
import org.apache.axis.MessageContext;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.description.OperationDesc;
import org.apache.axis.description.ParameterDesc;
import org.apache.axis.message.*;
import org.apache.axis.utils.Messages;
import org.apache.axis.utils.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import javax.xml.namespace.QName;
import javax.xml.rpc.JAXRPCException;
import javax.xml.rpc.ServiceException;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPException;
import java.nio.charset.StandardCharsets;
import java.rmi.RemoteException;
...
		try {
			Service s = new Service();
			Call call = (Call) service.createCall();
			call.setOperation(operation);
			call.setTargetEndpointAddress(wsdlUri);
			call.setTimeout(20000);  // 1 second, in miliseconds

			QName operationName = call.getOperationName();
			RPCElement body = new RPCElement(operationName.getNamespaceURI(), operationName.getLocalPart(), getParamList(para, call));
			MessageContext msgContext = call.getMessageContext();
			SOAPEnvelope reqEnv =
					new SOAPEnvelope(msgContext.getSOAPConstants(),
							msgContext.getSchemaVersion());
			body.setEncodingStyle(call.getEncodingStyle());
			Message reqMsg = call.getResponseMessage();
			call.setRequestMessage(reqMsg);

			reqEnv.addBodyElement(body);
//			result = (String) call.invoke(para);
			// 弃用axis调用webservice,改用httpclient方式直接调用
			result = doPostSoap1_2(wsdlUri, reqEnv.toString(), "");
			/*
			if (!StringUtils.isEmpty(result)){
				int beginIndex = result.indexOf("&lt");
				int endIndex = result.indexOf("</root>")+"</root>".length();
				result = result.substring(beginIndex, endIndex)
						.replace("&", "&")
						.replace("&lt;", "<")
						.replace("&gt;", ">")
						.replace("&quot;", "\"").replace("&apos;", "'");
			}*/

		} catch (Exception e) {
			throw e;
		}

到这里就完成了Httpclient对axis1.4的替换。

参考

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在 C# 中使用 `HttpClient` 调用 WebService 接口的方法如下: 1. 首先需要添加 `System.Net.Http` 和 `System.Web.Services` 引用。 2. 在代码中创建 `HttpClient` 实例,并设置请求头信息和请求内容。 ```csharp using System; using System.Net.Http; using System.Threading.Tasks; using System.Xml; namespace ConsoleApp1 { class Program { static async Task Main(string[] args) { // 创建 HttpClient 实例 HttpClient httpClient = new HttpClient(); // 设置请求头信息 httpClient.DefaultRequestHeaders.Add("SOAPAction", "http://tempuri.org/HelloWorld"); // 设置请求内容 string soapRequest = @"<?xml version=""1.0"" encoding=""utf-8""?> <soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""> <soap:Body> <HelloWorld xmlns=""http://tempuri.org/"" /> </soap:Body> </soap:Envelope>"; HttpContent httpContent = new StringContent(soapRequest, System.Text.Encoding.UTF8, "text/xml"); // 发送请求 HttpResponseMessage httpResponse = await httpClient.PostAsync("http://localhost:8080/HelloWorldService.asmx", httpContent); // 处理响应 if (httpResponse.IsSuccessStatusCode) { string soapResponse = await httpResponse.Content.ReadAsStringAsync(); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(soapResponse); string result = xmlDoc.GetElementsByTagName("HelloWorldResult")[0].InnerText; Console.WriteLine($"调用成功,返回结果:{result}"); } else { Console.WriteLine($"调用失败,响应状态码:{httpResponse.StatusCode}"); } } } } ``` 上述代码以调用 HelloWorld 接口为例,可以根据实际情况更改请求头信息和请求内容。此外,我们还需要处理响应。如果响应状态码为成功,我们可以解析出响应内容,并获取接口返回的结果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值