|
@@ -27,6 +27,7 @@ import org.apache.http.message.BasicNameValuePair;
|
|
|
import org.apache.http.protocol.HTTP;
|
|
|
import org.apache.http.ssl.SSLContextBuilder;
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.net.ssl.SSLContext;
|
|
|
import java.io.BufferedReader;
|
|
@@ -113,142 +114,131 @@ public class HttpClientUtils {
|
|
|
return sb.toString();
|
|
|
}
|
|
|
|
|
|
- public HttpClientEntity httpGetRequest(HttpClientEntity httpClientEntity) {
|
|
|
+ public HttpClientEntity httpGetRequest(HttpClientEntity httpClientEntity) throws Exception {
|
|
|
String result = null;
|
|
|
- try {
|
|
|
- CookieStore cs = httpClientEntity.getCookieStore();
|
|
|
- if (cs != null) {
|
|
|
- for (Cookie ck : cs.getCookies()) {
|
|
|
- cookieStore.addCookie(ck);
|
|
|
- }
|
|
|
+ CookieStore cs = httpClientEntity.getCookieStore();
|
|
|
+ if (cs != null) {
|
|
|
+ for (Cookie ck : cs.getCookies()) {
|
|
|
+ cookieStore.addCookie(ck);
|
|
|
}
|
|
|
- HttpResponse response = null;
|
|
|
- HttpGet httpGet = new HttpGet(httpClientEntity.getUrl());
|
|
|
+ }
|
|
|
+ HttpResponse response = null;
|
|
|
+ HttpGet httpGet = new HttpGet(httpClientEntity.getUrl());
|
|
|
|
|
|
- CloseableHttpClient httpClient = httpClientEntity.getCloseableHttpClient();
|
|
|
- response = httpClient.execute(httpGet);
|
|
|
- Header[] headers = response.getHeaders("Set-Cookie");
|
|
|
- if (headers != null && headers.length > 0) {
|
|
|
- for (Header header : headers) {
|
|
|
- String[] cookieArr = header.getValue().split(";");
|
|
|
- String[] ck = cookieArr[0].split("=");
|
|
|
- BasicClientCookie basicClientCookie = new BasicClientCookie(ck[0], ck[1]);
|
|
|
- Map<String, String> cookieMap = new HashMap<>();
|
|
|
- if (cookieArr.length > 1) {
|
|
|
- for (int i = 1; i < cookieArr.length; i++) {
|
|
|
- String[] ckParam = cookieArr[i].split("=");
|
|
|
- if (ckParam.length > 1) {
|
|
|
- cookieMap.put(ckParam[0].trim(), ckParam[1]);
|
|
|
- }
|
|
|
+ CloseableHttpClient httpClient = httpClientEntity.getCloseableHttpClient();
|
|
|
+ response = httpClient.execute(httpGet);
|
|
|
+ Header[] headers = response.getHeaders("Set-Cookie");
|
|
|
+ if (headers != null && headers.length > 0) {
|
|
|
+ for (Header header : headers) {
|
|
|
+ String[] cookieArr = header.getValue().split(";");
|
|
|
+ String[] ck = cookieArr[0].split("=");
|
|
|
+ BasicClientCookie basicClientCookie = new BasicClientCookie(ck[0], ck[1]);
|
|
|
+ Map<String, String> cookieMap = new HashMap<>();
|
|
|
+ if (cookieArr.length > 1) {
|
|
|
+ for (int i = 1; i < cookieArr.length; i++) {
|
|
|
+ String[] ckParam = cookieArr[i].split("=");
|
|
|
+ if (ckParam.length > 1) {
|
|
|
+ cookieMap.put(ckParam[0].trim(), ckParam[1]);
|
|
|
}
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- basicClientCookie.setPath(cookieMap.get("Path"));
|
|
|
- if (cookieMap.get("Domain") == null || cookieMap.get("Domain").equals("")) {
|
|
|
- if (httpClientEntity.getHeaders() != null && httpClientEntity.getHeaders().get("Origin") != null) {
|
|
|
- basicClientCookie.setDomain(httpClientEntity.getHeaders().get("Origin").split("//")[1]);
|
|
|
- }
|
|
|
- } else {
|
|
|
- basicClientCookie.setDomain(cookieMap.get("Domain"));
|
|
|
+ basicClientCookie.setPath(cookieMap.get("Path"));
|
|
|
+ if (cookieMap.get("Domain") == null || cookieMap.get("Domain").equals("")) {
|
|
|
+ if (httpClientEntity.getHeaders() != null && httpClientEntity.getHeaders().get("Origin") != null) {
|
|
|
+ basicClientCookie.setDomain(httpClientEntity.getHeaders().get("Origin").split("//")[1]);
|
|
|
}
|
|
|
- basicClientCookie.setExpiryDate(new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 365));
|
|
|
- cookieStore.addCookie(basicClientCookie);
|
|
|
+ } else {
|
|
|
+ basicClientCookie.setDomain(cookieMap.get("Domain"));
|
|
|
}
|
|
|
+ basicClientCookie.setExpiryDate(new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 365));
|
|
|
+ cookieStore.addCookie(basicClientCookie);
|
|
|
}
|
|
|
- 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();
|
|
|
- }
|
|
|
- log.info(httpClientEntity.getResult());
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ }
|
|
|
+ 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();
|
|
|
}
|
|
|
+ log.info(httpClientEntity.getResult());
|
|
|
httpClientEntity.setResult(result);
|
|
|
httpClientEntity.setCookieStore(cookieStore);
|
|
|
return httpClientEntity;
|
|
|
}
|
|
|
|
|
|
- public HttpClientEntity httpPostParamRequest(HttpClientEntity httpClientEntity) {
|
|
|
+ public HttpClientEntity httpPostParamRequest(HttpClientEntity httpClientEntity) throws Exception {
|
|
|
String result = "";
|
|
|
- try {
|
|
|
- CookieStore cs = httpClientEntity.getCookieStore();
|
|
|
- if (cs != null) {
|
|
|
- for (Cookie ck : cs.getCookies()) {
|
|
|
- cookieStore.addCookie(ck);
|
|
|
- }
|
|
|
+ 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, "UTF-8");
|
|
|
- httppost.setEntity(formEntity);
|
|
|
- CloseableHttpClient httpClient = httpClientEntity.getCloseableHttpClient();
|
|
|
- HttpResponse response = httpClient.execute(httppost);
|
|
|
- Header[] headers = response.getHeaders("Set-Cookie");
|
|
|
- if (headers != null && headers.length > 0) {
|
|
|
- for (Header header : headers) {
|
|
|
- String[] cookieArr = header.getValue().split(";");
|
|
|
- String[] ck = cookieArr[0].split("=");
|
|
|
- BasicClientCookie basicClientCookie = new BasicClientCookie(ck[0], ck[1]);
|
|
|
- Map<String, String> cookieMap = new HashMap<>();
|
|
|
- if (cookieArr.length > 1) {
|
|
|
- for (int i = 1; i < cookieArr.length; i++) {
|
|
|
- String[] ckParam = cookieArr[i].split("=");
|
|
|
- if (ckParam.length > 1) {
|
|
|
- cookieMap.put(ckParam[0].trim(), ckParam[1]);
|
|
|
- }
|
|
|
+ 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, "UTF-8");
|
|
|
+ httppost.setEntity(formEntity);
|
|
|
+ CloseableHttpClient httpClient = httpClientEntity.getCloseableHttpClient();
|
|
|
+ HttpResponse response = httpClient.execute(httppost);
|
|
|
+ Header[] headers = response.getHeaders("Set-Cookie");
|
|
|
+ if (headers != null && headers.length > 0) {
|
|
|
+ for (Header header : headers) {
|
|
|
+ String[] cookieArr = header.getValue().split(";");
|
|
|
+ String[] ck = cookieArr[0].split("=");
|
|
|
+ BasicClientCookie basicClientCookie = new BasicClientCookie(ck[0], ck[1]);
|
|
|
+ Map<String, String> cookieMap = new HashMap<>();
|
|
|
+ if (cookieArr.length > 1) {
|
|
|
+ for (int i = 1; i < cookieArr.length; i++) {
|
|
|
+ String[] ckParam = cookieArr[i].split("=");
|
|
|
+ if (ckParam.length > 1) {
|
|
|
+ cookieMap.put(ckParam[0].trim(), ckParam[1]);
|
|
|
}
|
|
|
}
|
|
|
- basicClientCookie.setPath(cookieMap.get("Path"));
|
|
|
- if (cookieMap.get("Domain") == null || cookieMap.get("Domain").equals("")) {
|
|
|
- basicClientCookie.setDomain(httpClientEntity.getHeaders().get("Origin").split("//")[1]);
|
|
|
- } else {
|
|
|
- basicClientCookie.setDomain(cookieMap.get("Domain"));
|
|
|
- }
|
|
|
- basicClientCookie.setExpiryDate(new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 365));
|
|
|
- cookieStore.addCookie(basicClientCookie);
|
|
|
}
|
|
|
+ basicClientCookie.setPath(cookieMap.get("Path"));
|
|
|
+ if (cookieMap.get("Domain") == null || cookieMap.get("Domain").equals("")) {
|
|
|
+ basicClientCookie.setDomain(httpClientEntity.getHeaders().get("Origin").split("//")[1]);
|
|
|
+ } else {
|
|
|
+ basicClientCookie.setDomain(cookieMap.get("Domain"));
|
|
|
+ }
|
|
|
+ basicClientCookie.setExpiryDate(new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 365));
|
|
|
+ cookieStore.addCookie(basicClientCookie);
|
|
|
}
|
|
|
- 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);
|
|
|
- result = EntityUtils.toString(response.getEntity());
|
|
|
- return httpPostParamRequest(httpClientEntity);
|
|
|
- } else if (statusCode == HttpStatus.SC_OK) {
|
|
|
- result = EntityUtils.toString(response.getEntity());
|
|
|
- log.info(result);
|
|
|
- httpClientEntity.setCloseableHttpClient(httpClient);
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- log.error(e.getMessage());
|
|
|
- return httpGetRequest(httpClientEntity);
|
|
|
+ }
|
|
|
+ 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);
|
|
|
+ result = EntityUtils.toString(response.getEntity());
|
|
|
+ return httpPostParamRequest(httpClientEntity);
|
|
|
+ } else if (statusCode == HttpStatus.SC_OK) {
|
|
|
+ result = EntityUtils.toString(response.getEntity());
|
|
|
+ log.info(result);
|
|
|
+ httpClientEntity.setCloseableHttpClient(httpClient);
|
|
|
}
|
|
|
httpClientEntity.setResult(result);
|
|
|
httpClientEntity.setCookieStore(cookieStore);
|
|
|
return httpClientEntity;
|
|
|
}
|
|
|
|
|
|
- public HttpClientEntity callingGraph(HttpClientEntity httpClientEntity, String json) {
|
|
|
+ public HttpClientEntity callingGraph(HttpClientEntity httpClientEntity, String json) throws Exception {
|
|
|
CookieStore cs = httpClientEntity.getCookieStore();
|
|
|
if (cs != null) {
|
|
|
for (Cookie ck : cs.getCookies()) {
|
|
@@ -262,53 +252,39 @@ public class HttpClientUtils {
|
|
|
httpPost.setHeader(key, httpClientEntity.getHeaders().get(key));
|
|
|
}
|
|
|
httpPost.setHeader("Content-Type", "application/json");
|
|
|
-
|
|
|
- try {
|
|
|
- StringEntity entity = new StringEntity(json, "utf-8");
|
|
|
- httpPost.setEntity(entity);
|
|
|
- httpPost.setProtocolVersion(HttpVersion.HTTP_1_0);
|
|
|
- httpPost.addHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE);
|
|
|
- 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);
|
|
|
-// }
|
|
|
- String result = EntityUtils.toString(response.getEntity(), "utf-8");
|
|
|
- Header[] headers = response.getHeaders("Set-Cookie");
|
|
|
- if (headers != null && headers.length > 0) {
|
|
|
- for (Header header : headers) {
|
|
|
- String[] cookieArr = header.getValue().split(";");
|
|
|
- String[] ck = cookieArr[0].split("=");
|
|
|
- BasicClientCookie basicClientCookie = new BasicClientCookie(ck[0], ck[1]);
|
|
|
- Map<String, String> cookieMap = new HashMap<>();
|
|
|
- if (cookieArr.length > 1) {
|
|
|
- for (int i = 1; i < cookieArr.length; i++) {
|
|
|
- String[] ckParam = cookieArr[i].split("=");
|
|
|
- if (ckParam.length > 1) {
|
|
|
- cookieMap.put(ckParam[0].trim(), ckParam[1]);
|
|
|
- }
|
|
|
+ StringEntity entity = new StringEntity(json, "utf-8");
|
|
|
+ httpPost.setEntity(entity);
|
|
|
+ httpPost.setProtocolVersion(HttpVersion.HTTP_1_0);
|
|
|
+ httpPost.addHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE);
|
|
|
+ response = httpClient.execute(httpPost);
|
|
|
+ String result = EntityUtils.toString(response.getEntity(), "utf-8");
|
|
|
+ Header[] headers = response.getHeaders("Set-Cookie");
|
|
|
+ if (headers != null && headers.length > 0) {
|
|
|
+ for (Header header : headers) {
|
|
|
+ String[] cookieArr = header.getValue().split(";");
|
|
|
+ String[] ck = cookieArr[0].split("=");
|
|
|
+ BasicClientCookie basicClientCookie = new BasicClientCookie(ck[0], ck[1]);
|
|
|
+ Map<String, String> cookieMap = new HashMap<>();
|
|
|
+ if (cookieArr.length > 1) {
|
|
|
+ for (int i = 1; i < cookieArr.length; i++) {
|
|
|
+ String[] ckParam = cookieArr[i].split("=");
|
|
|
+ if (ckParam.length > 1) {
|
|
|
+ cookieMap.put(ckParam[0].trim(), ckParam[1]);
|
|
|
}
|
|
|
}
|
|
|
- basicClientCookie.setPath(cookieMap.get("Path"));
|
|
|
- if (cookieMap.get("Domain") == null || cookieMap.get("Domain").equals("")) {
|
|
|
- basicClientCookie.setDomain(httpClientEntity.getHeaders().get("Origin").split("//")[1]);
|
|
|
- } else {
|
|
|
- basicClientCookie.setDomain(cookieMap.get("Domain"));
|
|
|
- }
|
|
|
- basicClientCookie.setExpiryDate(new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 365));
|
|
|
- cookieStore.addCookie(basicClientCookie);
|
|
|
}
|
|
|
+ basicClientCookie.setPath(cookieMap.get("Path"));
|
|
|
+ if (cookieMap.get("Domain") == null || cookieMap.get("Domain").equals("")) {
|
|
|
+ basicClientCookie.setDomain(httpClientEntity.getHeaders().get("Origin").split("//")[1]);
|
|
|
+ } else {
|
|
|
+ basicClientCookie.setDomain(cookieMap.get("Domain"));
|
|
|
+ }
|
|
|
+ basicClientCookie.setExpiryDate(new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 365));
|
|
|
+ cookieStore.addCookie(basicClientCookie);
|
|
|
}
|
|
|
-
|
|
|
-// httpClientEntity.setResult(builder.toString());
|
|
|
- httpClientEntity.setResult(result);
|
|
|
- log.info(httpClientEntity.getResult());
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return httpGetRequest(httpClientEntity);
|
|
|
}
|
|
|
+ httpClientEntity.setResult(result);
|
|
|
+ log.info(httpClientEntity.getResult());
|
|
|
httpClientEntity.setCookieStore(cookieStore);
|
|
|
return httpClientEntity;
|
|
|
}
|