|
|
|
@@ -423,64 +423,64 @@ public class HttpUtil { |
|
|
|
// return null; |
|
|
|
// } |
|
|
|
|
|
|
|
// /** |
|
|
|
// * post请求(用于请求json格式的参数) |
|
|
|
// * @param url |
|
|
|
// * @param params |
|
|
|
// * @return |
|
|
|
// */ |
|
|
|
// public static String doPost(String url, Map<String,String> headMap, String params){ |
|
|
|
// |
|
|
|
// CloseableHttpClient httpclient = HttpClients.createDefault(); |
|
|
|
// HttpPost httpPost = new HttpPost(url);// 创建httpPost |
|
|
|
// httpPost.setHeader("Accept", "application/json"); |
|
|
|
// httpPost.setHeader("Content-Type", "application/json"); |
|
|
|
// |
|
|
|
// if(headMap != null){ |
|
|
|
// Set<String> set = headMap.keySet(); |
|
|
|
// for(String key: set){ |
|
|
|
// httpPost.addHeader(key,headMap.get(key)); |
|
|
|
// } |
|
|
|
// } |
|
|
|
// |
|
|
|
// String charSet = "UTF-8"; |
|
|
|
// StringEntity entity = new StringEntity(params, charSet); |
|
|
|
// httpPost.setEntity(entity); |
|
|
|
// CloseableHttpResponse response = null; |
|
|
|
// |
|
|
|
// try { |
|
|
|
// response = httpclient.execute(httpPost); |
|
|
|
// StatusLine status = response.getStatusLine(); |
|
|
|
// int state = status.getStatusCode(); |
|
|
|
// if (state == HttpStatus.SC_OK) { |
|
|
|
// HttpEntity responseEntity = response.getEntity(); |
|
|
|
// String jsonString = EntityUtils.toString(responseEntity); |
|
|
|
// return jsonString; |
|
|
|
// } |
|
|
|
// else{ |
|
|
|
// logger.info("请求返回:"+state+"("+url+")"); |
|
|
|
// } |
|
|
|
// } |
|
|
|
// catch(Exception e){ |
|
|
|
// logger.error(e.getMessage()); |
|
|
|
// return null; |
|
|
|
// } |
|
|
|
// finally { |
|
|
|
// if (response != null) { |
|
|
|
// try { |
|
|
|
// response.close(); |
|
|
|
// } catch (IOException e) { |
|
|
|
// logger.error(e.getMessage()); |
|
|
|
// } |
|
|
|
// } |
|
|
|
// try { |
|
|
|
// httpclient.close(); |
|
|
|
// } catch (IOException e) { |
|
|
|
// logger.error(e.getMessage()); |
|
|
|
// } |
|
|
|
// } |
|
|
|
// return null; |
|
|
|
// } |
|
|
|
/** |
|
|
|
* post请求(用于请求json格式的参数) |
|
|
|
* @param url |
|
|
|
* @param params |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public static String doPost(String url, Map<String,String> headMap, String params){ |
|
|
|
|
|
|
|
CloseableHttpClient httpclient = HttpClients.createDefault(); |
|
|
|
HttpPost httpPost = new HttpPost(url);// 创建httpPost |
|
|
|
httpPost.setHeader("Accept", "application/json"); |
|
|
|
httpPost.setHeader("Content-Type", "application/json"); |
|
|
|
|
|
|
|
if(headMap != null){ |
|
|
|
Set<String> set = headMap.keySet(); |
|
|
|
for(String key: set){ |
|
|
|
httpPost.addHeader(key,headMap.get(key)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
String charSet = "UTF-8"; |
|
|
|
StringEntity entity = new StringEntity(params, charSet); |
|
|
|
httpPost.setEntity(entity); |
|
|
|
CloseableHttpResponse response = null; |
|
|
|
|
|
|
|
try { |
|
|
|
response = httpclient.execute(httpPost); |
|
|
|
StatusLine status = response.getStatusLine(); |
|
|
|
int state = status.getStatusCode(); |
|
|
|
if (state == HttpStatus.SC_OK) { |
|
|
|
HttpEntity responseEntity = response.getEntity(); |
|
|
|
String jsonString = EntityUtils.toString(responseEntity); |
|
|
|
return jsonString; |
|
|
|
} |
|
|
|
else{ |
|
|
|
logger.info("请求返回:"+state+"("+url+")"); |
|
|
|
} |
|
|
|
} |
|
|
|
catch(Exception e){ |
|
|
|
logger.error(e.getMessage()); |
|
|
|
return null; |
|
|
|
} |
|
|
|
finally { |
|
|
|
if (response != null) { |
|
|
|
try { |
|
|
|
response.close(); |
|
|
|
} catch (IOException e) { |
|
|
|
logger.error(e.getMessage()); |
|
|
|
} |
|
|
|
} |
|
|
|
try { |
|
|
|
httpclient.close(); |
|
|
|
} catch (IOException e) { |
|
|
|
logger.error(e.getMessage()); |
|
|
|
} |
|
|
|
} |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static String payPost(String url, String params) { |
|
|
|
@@ -504,8 +504,8 @@ public class HttpUtil { |
|
|
|
private static String exec(okhttp3.Request request) { |
|
|
|
try { |
|
|
|
okhttp3.Response response = OkHttpClient.newCall(request).execute(); |
|
|
|
String header = response.header("X-Tt-Logid"); |
|
|
|
logger.info("X-Tt-Logid="+header); |
|
|
|
// String header = response.header("X-Tt-Logid"); |
|
|
|
// logger.info("X-Tt-Logid="+header); |
|
|
|
if (!response.isSuccessful()) |
|
|
|
throw new RuntimeException("Unexpected code " + response); |
|
|
|
return response.body().string(); |
|
|
|
|