import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.http.HttpEntity;
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.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;
import org.apache.http.util.EntityUtils;
import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Slf4j
public class httpclient {
static CloseableHttpClient closeableHttpClient = null;
static CloseableHttpClient httpsClient = null;
static final String CHARSET = “UTF-8”;
static {
RequestConfig config = RequestConfig.custom().setConnectTimeout(5000).setSocketTimeout(500).build();
httpclient.closeableHttpClient = HttpClientBuilder.create().disableContentCompression().setDefaultRequestConfig(config).build();
createSSLHttpClient();
}
public static void createSSLHttpClient() {
try {
RequestConfig config = RequestConfig.custom().setConnectTimeout(5000).setSocketTimeout(500).build();
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
return true;
}
}).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
httpsClient = HttpClients.custom().setSSLSocketFactory(sslsf).setDefaultRequestConfig(config).build();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
}
}
public static String doPostByJson(String apiUrl, Object json) {
CloseableHttpClient httpClient = HttpClients.createDefault();
String httpStr = null;
HttpPost httpPost = new HttpPost(apiUrl);
CloseableHttpResponse response = null;
try {
StringEntity stringEntity = new StringEntity(json.toString(), "UTF-8");// 解决中文乱码问题
stringEntity.setContentEncoding("UTF-8");
stringEntity.setContentType("application/json");
httpPost.setEntity(stringEntity);
httpPost.addHeader("username", "acrel");
httpPost.addHeader("password", "acrel001");
response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
httpStr = EntityUtils.toString(entity, "UTF-8");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (response != null) {
try {
EntityUtils.consume(response.getEntity());
} catch (IOException e) {
e.printStackTrace();
}
}
}
return httpStr;
}
public static List<String> load(final String vuserno) {
try {
String url = "http://192.168.xxx.xxx:8007/cuDeviceType/getDevTypeAndDeviceTree";
List<NameValuePair> pairs = new ArrayList<NameValuePair>() {{
add(new BasicNameValuePair("customerCode", vuserno));
add(new BasicNameValuePair("flag", "3"));
}};
url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(pairs, "utf-8"));
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse response = closeableHttpClient.execute(httpGet);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
httpGet.abort();
throw new RuntimeException("请求失败,错误码:" + statusCode);
}
HttpEntity entity = response.getEntity();
String result = null;
if (entity != null) {
result = EntityUtils.toString(entity, CHARSET);
}
System.out.println(result);
EntityUtils.consume(entity);
response.close();
JSONObject jsonObject = JSON.parseObject(result);
String rest2 = jsonObject.getString("data");
if (rest2.length() == 0) {
return null;
}
List<String> concodelist = Arrays.asList(rest2.split(","));
return concodelist;
} catch (Exception e) {
log.info("http链接" + e.getMessage());
}
return null;
}
public static List<String> loads(String vuserno,String Cudate) {
try {
String url = "https://www.xxxxxx.cn/IECP/dataToMonthCompany";
List<NameValuePair> pairs = new ArrayList<NameValuePair>() {{
add(new BasicNameValuePair("customer", vuserno));
add(new BasicNameValuePair("month", Cudate));
}};
url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(pairs, "utf-8"));
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse response = httpsClient.execute(httpGet);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
httpGet.abort();
throw new RuntimeException("请求失败,错误码:" + statusCode);
}
HttpEntity entity = response.getEntity();
String result = null;
if (entity != null) {
result = EntityUtils.toString(entity, CHARSET);
}
System.out.println(result);
EntityUtils.consume(entity);
response.close();
JSONObject jsonObject = JSON.parseObject(result);
String rest2 = jsonObject.getString("data");
if (rest2.length() == 0) {
return null;
}
else {
String newString = rest2.replace("[\"","");
rest2= newString.replaceAll("\"]","");
}
List<String> concodelist = Arrays.asList(rest2.split(","));
return concodelist;
} catch (Exception e) {
log.info("http链接" + e.getMessage());
}
return null;
}
}