package com.itstyle.mdm.utils;
|
|
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONObject;
|
import com.itstyle.mdm.entity.HttpClientResult;
|
import org.apache.http.HttpStatus;
|
import org.apache.http.NameValuePair;
|
import org.apache.http.client.config.RequestConfig;
|
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
|
import org.apache.http.client.methods.HttpPost;
|
import org.apache.http.client.methods.HttpRequestBase;
|
import org.apache.http.entity.StringEntity;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.HttpClients;
|
import org.apache.http.message.BasicNameValuePair;
|
import org.apache.http.util.EntityUtils;
|
|
import java.io.IOException;
|
import java.io.UnsupportedEncodingException;
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Set;
|
|
/**
|
* 主数据API接口通用类
|
*/
|
public class MdmApiUtils {
|
|
// 编码格式。发送编码格式统一用UTF-8
|
private static final String ENCODING = "UTF-8";
|
|
// 设置连接超时时间,单位毫秒。
|
private static final int CONNECT_TIMEOUT = 10000;
|
|
// 请求获取数据的超时时间(即响应时间),单位毫秒。
|
private static final int SOCKET_TIMEOUT = 10000;
|
|
/**
|
* 发送post请求;带请求头和请求参数
|
*
|
* @param url 请求地址
|
* @param headers 请求头集合
|
* @param params 请求参数集合
|
* @return
|
* @throws Exception
|
*/
|
public static HttpClientResult doPost(String url, Map<String, String> headers, Map<String, String> params) throws Exception {
|
// 创建httpClient对象
|
CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
// 创建http对象
|
HttpPost httpPost = new HttpPost(url);
|
/**
|
* setConnectTimeout:设置连接超时时间,单位毫秒。
|
* setConnectionRequestTimeout:设置从connect Manager(连接池)获取Connection
|
* 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。
|
* setSocketTimeout:请求获取数据的超时时间(即响应时间),单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。
|
*/
|
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build();
|
httpPost.setConfig(requestConfig);
|
// 设置请求头
|
/*httpPost.setHeader("Cookie", "");
|
httpPost.setHeader("Connection", "keep-alive");
|
httpPost.setHeader("Accept", "application/json");
|
httpPost.setHeader("Accept-Language", "zh-CN,zh;q=0.9");
|
httpPost.setHeader("Accept-Encoding", "gzip, deflate, br");
|
httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");*/
|
packageHeader(headers, httpPost);
|
|
// 封装请求参数
|
packageParam(params, httpPost);
|
|
// 创建httpResponse对象
|
CloseableHttpResponse httpResponse = null;
|
|
try {
|
// 执行请求并获得响应结果
|
return getHttpClientResult(httpResponse, httpClient, httpPost);
|
} finally {
|
// 释放资源
|
release(httpResponse, httpClient);
|
}
|
}
|
|
/**
|
* Description: 封装请求头
|
* @param params
|
* @param httpMethod
|
*/
|
public static void packageHeader(Map<String, String> params, HttpRequestBase httpMethod) {
|
// 封装请求头
|
if (params != null) {
|
Set<Map.Entry<String, String>> entrySet = params.entrySet();
|
for (Map.Entry<String, String> entry : entrySet) {
|
// 设置到请求头到HttpRequestBase对象中
|
httpMethod.setHeader(entry.getKey(), entry.getValue());
|
}
|
}
|
}
|
|
/**
|
* Description: 封装请求参数
|
*
|
* @param params
|
* @param httpMethod
|
* @throws UnsupportedEncodingException
|
*/
|
public static void packageParam(Map<String, String> params, HttpEntityEnclosingRequestBase httpMethod)
|
throws UnsupportedEncodingException {
|
// 封装请求参数
|
if (params != null) {
|
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
|
Set<Map.Entry<String, String>> entrySet = params.entrySet();
|
for (Map.Entry<String, String> entry : entrySet) {
|
nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
|
}
|
|
// 设置到请求的http对象中
|
httpMethod.setEntity(new UrlEncodedFormEntity(nvps, ENCODING));
|
}
|
}
|
|
/**
|
*
|
* @param url
|
* @param json
|
* @return
|
* @throws Exception
|
*/
|
public static HttpClientResult doPostJson(String url, String json) throws Exception{
|
// 创建httpClient对象
|
CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
//第二步:创建httpPost对象
|
HttpPost httpPost = new HttpPost(url);
|
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build();
|
httpPost.setConfig(requestConfig);
|
//第三步:给httpPost设置JSON格式的参数
|
StringEntity requestEntity = new StringEntity(json,"utf-8");
|
requestEntity.setContentEncoding("UTF-8");
|
httpPost.setHeader("Content-type", "application/json");
|
httpPost.setEntity(requestEntity);
|
|
// 创建httpResponse对象
|
CloseableHttpResponse httpResponse = null;
|
|
try {
|
// 执行请求并获得响应结果
|
return getHttpClientResult(httpResponse, httpClient, httpPost);
|
} finally {
|
// 释放资源
|
release(httpResponse, httpClient);
|
}
|
}
|
|
public static String getMesJson(String json){
|
JSONObject mesResJson=new JSONObject();
|
JSONObject jsonobj=JSONObject.parseObject(json);
|
JSONArray jsonArray = jsonobj.getJSONArray("records");
|
JSONArray mesResDetail=new JSONArray();
|
for(int i=0;i<jsonArray.size();i++){
|
JSONObject rowObject = jsonArray.getJSONObject(i);
|
JSONObject tmp1=new JSONObject();
|
tmp1.put("Tgcoriginalcode",rowObject.getString("tgctwoclasscode"));
|
JSONObject tmp2=new JSONObject();
|
tmp2.put("Code",rowObject.getString("code"));
|
JSONObject tmp3=new JSONObject();
|
tmp3.put("Name",rowObject.getString("name"));
|
JSONObject tmp4=new JSONObject();
|
tmp4.put("Specification",rowObject.getString("specification"));
|
JSONObject tmp5=new JSONObject();
|
tmp5.put("UnitName",rowObject.getString("unitName"));
|
JSONObject tmp6=new JSONObject();
|
tmp6.put("TypeCode",rowObject.getString("typeCode"));
|
JSONObject tmp7=new JSONObject();
|
tmp7.put("SpecificationDisplayName",rowObject.getString("specificationDisplayName"));
|
JSONObject tmp8=new JSONObject();
|
tmp8.put("SupplierName",rowObject.getString("supplierName"));
|
mesResDetail.add(tmp1);
|
mesResDetail.add(tmp2);
|
mesResDetail.add(tmp3);
|
mesResDetail.add(tmp4);
|
mesResDetail.add(tmp5);
|
mesResDetail.add(tmp6);
|
mesResDetail.add(tmp7);
|
mesResDetail.add(tmp8);
|
}
|
mesResJson.put("Records",mesResDetail);
|
return mesResJson.toJSONString();
|
}
|
public static String getMesSoapBody(String json){
|
return "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
|
"<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\n" +
|
" <soap12:Body>\n" +
|
" <ReceiveTCG xmlns=\"http://www.hitachi.com/HITPHAMS\">\n" +
|
" <JsonStr><![CDATA["+json+"]]></JsonStr>\n" +
|
" </ReceiveTCG>\n" +
|
" </soap12:Body>\n" +
|
"</soap12:Envelope>";
|
}
|
public static HttpClientResult doPostXmlJson(String url, String xmlStr) throws Exception{
|
// 创建httpClient对象
|
CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
//第二步:创建httpPost对象
|
HttpPost httpPost = new HttpPost(url);
|
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build();
|
httpPost.setConfig(requestConfig);
|
//第三步:给httpPost设置JSON格式的参数
|
StringEntity requestEntity = new StringEntity(xmlStr,"utf-8");
|
requestEntity.setContentEncoding("UTF-8");
|
httpPost.setHeader("Content-type", "application/soap+xml; charset=utf-8");
|
httpPost.setEntity(requestEntity);
|
|
// 创建httpResponse对象
|
CloseableHttpResponse httpResponse = null;
|
|
try {
|
// 执行请求并获得响应结果
|
return getHttpClientResult(httpResponse, httpClient, httpPost);
|
} finally {
|
// 释放资源
|
release(httpResponse, httpClient);
|
}
|
}
|
|
/**
|
* Description: 获得响应结果
|
*
|
* @param httpResponse
|
* @param httpClient
|
* @param httpMethod
|
* @return
|
* @throws Exception
|
*/
|
public static HttpClientResult getHttpClientResult(CloseableHttpResponse httpResponse,
|
CloseableHttpClient httpClient, HttpRequestBase httpMethod) throws Exception {
|
// 执行请求
|
httpResponse = httpClient.execute(httpMethod);
|
|
// 获取返回结果
|
if (httpResponse != null && httpResponse.getStatusLine() != null) {
|
String content = "";
|
if (httpResponse.getEntity() != null) {
|
content = EntityUtils.toString(httpResponse.getEntity(), ENCODING);
|
}
|
return new HttpClientResult(httpResponse.getStatusLine().getStatusCode(), content);
|
}
|
return new HttpClientResult(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
}
|
|
/**
|
* Description: 释放资源
|
*
|
* @param httpResponse
|
* @param httpClient
|
* @throws IOException
|
*/
|
public static void release(CloseableHttpResponse httpResponse, CloseableHttpClient httpClient) throws IOException {
|
// 释放资源
|
if (httpResponse != null) {
|
httpResponse.close();
|
}
|
if (httpClient != null) {
|
httpClient.close();
|
}
|
}
|
}
|