|
@@ -0,0 +1,171 @@
|
|
|
+package cn.com.ctop.common.module.utils;
|
|
|
+
|
|
|
+import cn.com.ctop.common.module.entity.HttpClientEntity;
|
|
|
+import com.google.gson.Gson;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.http.Header;
|
|
|
+import org.apache.http.HttpResponse;
|
|
|
+import org.apache.http.HttpStatus;
|
|
|
+import org.apache.http.NameValuePair;
|
|
|
+import org.apache.http.client.CookieStore;
|
|
|
+import org.apache.http.client.HttpClient;
|
|
|
+import org.apache.http.client.config.CookieSpecs;
|
|
|
+import org.apache.http.client.config.RequestConfig;
|
|
|
+import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
|
+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.conn.ssl.TrustStrategy;
|
|
|
+import org.apache.http.cookie.Cookie;
|
|
|
+import org.apache.http.entity.StringEntity;
|
|
|
+import org.apache.http.impl.client.BasicCookieStore;
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
+import org.apache.http.message.BasicNameValuePair;
|
|
|
+import org.apache.http.ssl.SSLContextBuilder;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
+
|
|
|
+import javax.net.ssl.SSLContext;
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.security.cert.CertificateException;
|
|
|
+import java.security.cert.X509Certificate;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+public class HttpClientUtils {
|
|
|
+ public CookieStore cookieStore = new BasicCookieStore();
|
|
|
+
|
|
|
+ public CloseableHttpClient createSSLClientDefault() {
|
|
|
+ try {
|
|
|
+ SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
|
|
|
+ //信任所有证书
|
|
|
+ @Override
|
|
|
+ public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }).build();
|
|
|
+ RequestConfig globalConfig = RequestConfig.custom().setConnectTimeout(60000).setSocketTimeout(60000).setCookieSpec(CookieSpecs.STANDARD).build();
|
|
|
+ SSLConnectionSocketFactory sslFactory = new SSLConnectionSocketFactory(sslContext);
|
|
|
+ return HttpClients.custom().setDefaultCookieStore(cookieStore).setDefaultRequestConfig(globalConfig).setConnectionReuseStrategy((response, context) -> false).setSSLSocketFactory(sslFactory).build();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("处理Https证书异常", e);
|
|
|
+ }
|
|
|
+ return HttpClients.createDefault();
|
|
|
+ }
|
|
|
+
|
|
|
+ public HttpClientEntity httpGetRequest(HttpClientEntity httpClientEntity) {
|
|
|
+ try {
|
|
|
+ CookieStore cs = httpClientEntity.getCookieStore();
|
|
|
+ if (cs != null) {
|
|
|
+ for (Cookie ck : cs.getCookies()) {
|
|
|
+ cookieStore.addCookie(ck);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ HttpResponse response = null;
|
|
|
+ HttpGet httpGet = new HttpGet(httpClientEntity.getUrl());
|
|
|
+ String result = null;
|
|
|
+ CloseableHttpClient httpClient = httpClientEntity.getCloseableHttpClient();
|
|
|
+ response = httpClient.execute(httpGet);
|
|
|
+
|
|
|
+ int statusCode = response.getStatusLine().getStatusCode();
|
|
|
+ if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
|
|
|
+ String newUrl = response.getFirstHeader("Location").getValue();
|
|
|
+ httpClientEntity.setUrl(newUrl);
|
|
|
+ return httpGetRequest(httpClientEntity);
|
|
|
+ } else if (statusCode == HttpStatus.SC_OK) {
|
|
|
+ BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
|
|
|
+ String line = null;
|
|
|
+ StringBuilder builder = new StringBuilder();
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
+ builder.append(line);
|
|
|
+ }
|
|
|
+ result = builder.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ httpClientEntity.setCookieStore(cookieStore);
|
|
|
+ return httpClientEntity;
|
|
|
+ }
|
|
|
+
|
|
|
+ public HttpClientEntity httpPostParamRequest(HttpClientEntity httpClientEntity) {
|
|
|
+ try {
|
|
|
+ CookieStore cs = httpClientEntity.getCookieStore();
|
|
|
+ if (cs != null) {
|
|
|
+ for (Cookie ck : cs.getCookies()) {
|
|
|
+ cookieStore.addCookie(ck);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ HttpPost httppost = new HttpPost(httpClientEntity.getUrl());
|
|
|
+ for (String key : httpClientEntity.getHeaders().keySet()) {
|
|
|
+ httppost.setHeader(key, httpClientEntity.getHeaders().get(key));
|
|
|
+ }
|
|
|
+ List<NameValuePair> paramList = new ArrayList<NameValuePair>();
|
|
|
+ for (String key : httpClientEntity.getParams().keySet()) {
|
|
|
+ BasicNameValuePair basicNameValuePair = new BasicNameValuePair(key, String.valueOf(httpClientEntity.getParams().get(key)));
|
|
|
+ paramList.add(basicNameValuePair);
|
|
|
+ }
|
|
|
+ UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(paramList);
|
|
|
+ httppost.setEntity(formEntity);
|
|
|
+ CloseableHttpClient httpClient = httpClientEntity.getCloseableHttpClient();
|
|
|
+ HttpResponse response = httpClient.execute(httppost);
|
|
|
+
|
|
|
+ int statusCode = response.getStatusLine().getStatusCode();
|
|
|
+ if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
|
|
|
+ String newUrl = response.getFirstHeader("Location").getValue();
|
|
|
+ httpClientEntity.setUrl(newUrl);
|
|
|
+ return httpPostParamRequest(httpClientEntity);
|
|
|
+ } else if (statusCode == HttpStatus.SC_OK) {
|
|
|
+ String result = EntityUtils.toString(response.getEntity());
|
|
|
+ log.info(result);
|
|
|
+ httpClientEntity.setResult(result);
|
|
|
+ httpClientEntity.setCloseableHttpClient(httpClient);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ log.error(e.getMessage());
|
|
|
+ }
|
|
|
+ System.out.println(new Gson().toJson(cookieStore.getCookies()));
|
|
|
+ httpClientEntity.setCookieStore(cookieStore);
|
|
|
+ return httpClientEntity;
|
|
|
+ }
|
|
|
+
|
|
|
+ public HttpClientEntity callingGraph(HttpClientEntity httpClientEntity, String json) {
|
|
|
+ CookieStore cs = httpClientEntity.getCookieStore();
|
|
|
+ if (cs != null) {
|
|
|
+ for (Cookie ck : cs.getCookies()) {
|
|
|
+ cookieStore.addCookie(ck);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ HttpClient httpClient = httpClientEntity.getCloseableHttpClient();
|
|
|
+ HttpResponse response = null;
|
|
|
+ HttpPost httpPost = new HttpPost(httpClientEntity.getUrl());
|
|
|
+ for (String key : httpClientEntity.getHeaders().keySet()) {
|
|
|
+ httpPost.setHeader(key, httpClientEntity.getHeaders().get(key));
|
|
|
+ }
|
|
|
+ httpPost.setHeader("Content-Type", "application/json");
|
|
|
+
|
|
|
+ try {
|
|
|
+ StringEntity entity = new StringEntity(json, "utf-8");
|
|
|
+ httpPost.setEntity(entity);
|
|
|
+ response = httpClient.execute(httpPost);
|
|
|
+
|
|
|
+ BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
|
|
|
+ String line = null;
|
|
|
+ StringBuilder builder = new StringBuilder();
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
+ builder.append(line);
|
|
|
+ }
|
|
|
+ httpClientEntity.setResult(builder.toString());
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ httpClientEntity.setCookieStore(cookieStore);
|
|
|
+ System.out.println(new Gson().toJson(cookieStore.getCookies()));
|
|
|
+ return httpClientEntity;
|
|
|
+ }
|
|
|
+}
|