//请求json
public static String HttpsPost(String url, Map<String, Object> param) throws Exception{
String respContent ="";
String restServiceURL = url;
System.out.println(restServiceURL);
CloseableHttpClient client = HttpClients.createDefault();
//设置传入参数
StringEntity entity = new StringEntity(JSONObject.toJSONString(param), "utf-8");
HttpPost httpGet = new HttpPost(restServiceURL);
httpGet.addHeader("Content-Type","application/json;charset=UTF-8");
httpGet.setEntity(entity);
HttpResponse resp = client.execute(httpGet);
int statusCode = resp.getStatusLine().getStatusCode();
if ( 200==statusCode ) {
HttpEntity he = resp.getEntity();
respContent = EntityUtils.toString(he, "utf-8");
}else {
log.error("请求第三方异常,状态码{},地址{}",statusCode,url);
}
return respContent;
}
/**
* http请求 application/x-www-form-urlencoded
*/
public static String HttpsPostXw(String url, Map<String, String> param) throws Exception{
String respContent ="";
String restServiceURL = url;
CloseableHttpClient client = HttpClients.createDefault();
//设置传入参数
HttpPost httpGet = new HttpPost(restServiceURL);
httpGet.setHeader("Content-type", "application/x-www-form-urlencoded");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
if(param!=null){
for (Map.Entry<String, String> entry : param.entrySet()) {
nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
//设置参数到请求对象中
httpGet.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
//装填参数
if(param!=null){
for (Map.Entry<String, String> entry : param.entrySet()) {
httpGet.setHeader(entry.getKey(),entry.getValue());
}
}
HttpResponse resp = client.execute(httpGet);
int statusCode = resp.getStatusLine().getStatusCode();
if ( 200==statusCode ) {
HttpEntity he = resp.getEntity();
respContent = EntityUtils.toString(he, "utf-8");
}else {
log.error("请求第三方异常,状态码{},地址{}",statusCode,url);
}
return respContent;
}