Commit ae7f4235 zshaohui

http增加调用

1 个父辈 3785f772
......@@ -34,6 +34,7 @@ import java.io.*;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
......@@ -391,6 +392,53 @@ public class HttpHelper {
}
}
public static String post(String url, Map<String, Object> paramMap, Map<String, String> headerMap) throws Exception {
HttpPost httpPost = new HttpPost(url);
if (headerMap != null && !headerMap.isEmpty()) {
for (Map.Entry<String, String> header : headerMap.entrySet()) {
httpPost.addHeader(header.getKey(), header.getValue());
}
}
// 设置请求参数
try {
String param = "";
if (paramMap != null && paramMap.size() > 0) {
Iterator<String> ite = paramMap.keySet().iterator();
while (ite.hasNext()) {
String key = ite.next();// key
Object valueObj = paramMap.get(key);
String value = "";
if (valueObj != null) {
value = valueObj.toString();
}
param += key + "=" + value + "&";
}
param = param.substring(0, param.length() - 1);
}
param = URLDecoder.decode(param, "UTF-8");
httpPost.setEntity(new StringEntity(param, "UTF-8"));
RequestConfig defaultConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build();
httpPost.setConfig(defaultConfig);
} catch (Exception e) {
log.error("Request params to [" + url + "] json exception:" + e.getMessage());
}
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
try (CloseableHttpClient httpClient = httpClientBuilder.setSSLSocketFactory(getSslConnectionSocketFactory()).build()) {
CloseableHttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
String responseContent = EntityUtils.toString(entity, "UTF-8");
response.close();
httpClient.close();
log.info("postJsonWithAuth url=[" + url + "] return= [" + responseContent + "]");
return responseContent;
} catch (Exception e) {
log.error("Request to [" + url + "] failed:" + e.getMessage());
}
return "";
}
private static String appendString(String url, String paramStr) {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(url);
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!