HttpClient get调用wsdl接口

pom.xml中引入

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

   package com.ltit.httpclient;
	import java.io.IOException;
	import java.io.InputStream;
	
	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.HttpGet;
	import org.apache.http.impl.client.CloseableHttpClient;
	import org.apache.http.impl.client.HttpClientBuilder;
	import org.apache.http.impl.client.HttpClients;
	import org.springframework.stereotype.Service;
	
	import com.alibaba.fastjson.JSONObject;
	import com.ltit.common.Response;
	import com.ltit.common.util.CommonUtils;
	
	import net.sf.json.JSON;
	
	@Service
	public class CHttpClient {
		public Object requestForObject(String url) throws UnsupportedOperationException, IOException {
			CloseableHttpClient httpClient = HttpClients.createDefault();
			//String  url="http://frp.pumelo.io:7777/WebServiceToLitong/RtdbDataService.asmx/ReadTagHisValuesInTime?tagName=发电量&span=1&sTime=2019-04-01&eTime=2019-04-01";
			HttpGet httpGet = new HttpGet(url);
			CloseableHttpResponse httpResponse = null;
			RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectionRequestTimeout(2000)
					.build();
			httpGet.setConfig(requestConfig);
			httpResponse = httpClient.execute(httpGet);
			InputStream inputStream = null;
			System.out.println(+httpResponse.getStatusLine().getStatusCode());
			if (httpResponse.getStatusLine().getStatusCode() == 200) {
				HttpEntity httpResponseEntity = httpResponse.getEntity();
				//            
				// System.out.println("响应内容:"+EntityUtils.toString(httpResponseEntity));//entity的流不可重复读,查看源码可以发现此处流已关闭
				inputStream = httpResponseEntity.getContent();
				JSON json=CommonUtils.ConvertXMLtoJSON(inputStream);
				JSONObject jsonObject=JSONObject.parseObject(json.toString());
				
				return jsonObject;
				/*
				 * BufferedReader bufferedReader = new BufferedReader(new
				 * InputStreamReader(inputStream)); String line = null; StringBuffer
				 * stringBuffer = new StringBuffer(); while ((line = bufferedReader.readLine())
				 * != null) { stringBuffer.append(line); } System.out.println("----------" +
				 * stringBuffer.toString() + "---------"); return String.valueOf(stringBuffer);
				 */
			} else {
				System.out.println("请求出错!");
			}
			return null;
		}
	}

xml转json格式

public static JSON ConvertXMLtoJSON(InputStream is) {
	String xml;
	try {
		xml = IOUtils.toString(is);
		System.out.println(xml);
		XMLSerializer xmlSerializer = new XMLSerializer();
		JSON json = xmlSerializer.read(xml);
		System.out.println(json);
		System.out.println(json.toString(0));
		return json;
	} catch (IOException e) {
		e.printStackTrace();
	}
	return null;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用 HttpClient 调用 WSDL,您需要执行以下步骤: 1.导入 WSDL 文件:使用 Visual Studio 或任何 WSDL 工具来导入 WSDL 文件并生成客户端代理类。 2.创建 HttpClient 实例:在您的代码中创建 HttpClient 实例。 3.创建请求消息:使用客户端代理类创建请求消息。 4.发送请求:使用 HttpClient 实例发送请求消息。 5.处理响应:在您的代码中处理响应消息。 以下是示例代码: ```csharp // 导入 WSDL 并生成客户端代理类 MyWebServiceClient client = new MyWebServiceClient(); // 创建 HttpClient 实例 HttpClient httpClient = new HttpClient(); // 创建请求消息 HttpRequestMessage request = new HttpRequestMessage(); request.Method = HttpMethod.Post; request.RequestUri = new Uri(client.Endpoint.Address.ToString()); request.Headers.Add("SOAPAction", client.Endpoint.Address + "/MyMethod"); // 将 SOAP 请求消息添加到请求消息中 StreamContent streamContent = new StreamContent(client.InnerChannel.RequestMessage.GetReaderAtBodyContents().ReadInnerXmlAsStream()); streamContent.Headers.ContentType = new MediaTypeHeaderValue("text/xml"); request.Content = streamContent; // 发送请求 HttpResponseMessage response = await httpClient.SendAsync(request); // 处理响应 string responseString = await response.Content.ReadAsStringAsync(); ``` 请注意,此示例仅适用于基于 SOAP 的 WSDL。如果您的 WSDL 使用 REST,您需要使用不同的方法来处理请求和响应。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值