|
|
@@ -132,6 +132,48 @@ public class HttpUtils {
|
|
|
return strReturn;
|
|
|
}
|
|
|
|
|
|
+ public static String httpPostRequest(String url, Map<String, Object> param, Map<String, String> headers, int retry) {
|
|
|
+ HttpClient httpClient = createSslClientDefault();
|
|
|
+ String strReturn = "";
|
|
|
+ try {
|
|
|
+ HttpPost httppost = new HttpPost(url);
|
|
|
+ httppost.setHeader("User-Agent", USER_AGENT);
|
|
|
+ httppost.addHeader("Content-Type", "application/json");
|
|
|
+ if (headers != null && headers.size() > 0) {
|
|
|
+ for (String key : headers.keySet()) {
|
|
|
+ httppost.setHeader(key, headers.get(key));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (param != null && param.size() > 0) {
|
|
|
+ httppost.setEntity(new StringEntity(new Gson().toJson(param), Charset.forName("UTF-8")));
|
|
|
+ }
|
|
|
+ RequestConfig requestConfig =
|
|
|
+ RequestConfig.custom().setSocketTimeout(300 * 1000).setConnectTimeout(300 * 1000).build();
|
|
|
+ httppost.setConfig(requestConfig);
|
|
|
+ HttpEntity respentity;
|
|
|
+
|
|
|
+ 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();
|
|
|
+ strReturn = httpPostRequest(newUrl, param, headers);
|
|
|
+ } else if (statusCode == HttpStatus.SC_OK) {
|
|
|
+ respentity = response.getEntity();
|
|
|
+ strReturn = EntityUtils.toString(respentity,"UTF-8");
|
|
|
+ } else if (retry > 0) {
|
|
|
+ strReturn = httpPostRequest(url, param, headers, retry - 1);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ log.error(e.getMessage());
|
|
|
+ if (retry > 0) {
|
|
|
+ strReturn = httpPostRequest(url, param, headers, retry - 1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return strReturn;
|
|
|
+ }
|
|
|
+
|
|
|
public static JSONObject bytedanceGetRequest(String accessToken, String url, JSONObject params) {
|
|
|
// 构造请求
|
|
|
HttpEntityEnclosingRequestBase httpEntity = new HttpEntityEnclosingRequestBase() {
|