HttpClient get调用wsdl接口

pom.xml中引入


      org.apache.httpcomponents
      httpclient
      4.5.5

   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;
}

你可能感兴趣的:(HttpClient,get调用wsdl接口)