HttpClient get呼び出しwsdlインタフェース
2904 ワード
pom.xmlにorgを導入する.apache.httpcomponents httpclient 4.5.5
xml変換jsonフォーマット
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;
}