|
@ -1,9 +1,5 @@
|
|
|
package com.yihu.wlyy.util;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
import java.util.List;
|
|
|
|
|
|
import org.apache.http.HttpEntity;
|
|
|
import org.apache.http.NameValuePair;
|
|
|
import org.apache.http.ParseException;
|
|
@ -14,12 +10,20 @@ import org.apache.http.client.methods.HttpGet;
|
|
|
import org.apache.http.client.methods.HttpPost;
|
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
import org.apache.http.impl.client.HttpClients;
|
|
|
import org.apache.http.message.BasicNameValuePair;
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
import org.json.JSONObject;
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
import org.springframework.http.MediaType;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
public class HttpClientUtil {
|
|
|
|
|
|
/**
|
|
@ -103,6 +107,71 @@ public class HttpClientUtil {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* http调用方法,(健康之路开放平台)
|
|
|
* @param url
|
|
|
* @param params
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
public static String httpPost(String url, Map<String, String> params) throws Exception {
|
|
|
CloseableHttpClient httpclient = HttpClients.createDefault();
|
|
|
try {
|
|
|
HttpPost httpPost = new HttpPost(url);
|
|
|
if(params!=null&¶ms.size()>0){
|
|
|
List<NameValuePair> valuePairs = new ArrayList<NameValuePair>(params.size());
|
|
|
for (Map.Entry<String, String> entry : params.entrySet()) {
|
|
|
NameValuePair nameValuePair = new BasicNameValuePair(entry.getKey(), String.valueOf(entry.getValue()));
|
|
|
valuePairs.add(nameValuePair);
|
|
|
}
|
|
|
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity( valuePairs, "UTF-8");
|
|
|
httpPost.setEntity(formEntity);
|
|
|
}
|
|
|
CloseableHttpResponse resp = httpclient.execute(httpPost);
|
|
|
try {
|
|
|
HttpEntity entity = resp.getEntity();
|
|
|
String respContent = EntityUtils.toString(entity, "UTF-8").trim();
|
|
|
return respContent;
|
|
|
} finally {
|
|
|
resp.close();
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
return null;
|
|
|
}finally {
|
|
|
httpclient.close();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取加密后参数集合(健康之路开放平台)
|
|
|
* @param params
|
|
|
* @return
|
|
|
*/
|
|
|
public static Map<String,String> getSecretParams(Map<String, String> params,String appId,String secret)
|
|
|
{
|
|
|
String timestamp = Long.toString(System.currentTimeMillis());
|
|
|
params.put("timestamp", timestamp);
|
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
|
|
|
|
// 对参数名进行字典排序
|
|
|
String[] keyArray = params.keySet().toArray(new String[0]);
|
|
|
Arrays.sort(keyArray);
|
|
|
// 拼接有序的参数名-值串
|
|
|
stringBuilder.append(appId);
|
|
|
for(String key : keyArray)
|
|
|
{
|
|
|
stringBuilder.append(key).append(params.get(key));
|
|
|
}
|
|
|
String codes = stringBuilder.append(secret).toString();
|
|
|
String sign = org.apache.commons.codec.digest.DigestUtils.shaHex(codes).toUpperCase();
|
|
|
// 添加签名,并发送请求
|
|
|
params.put("appId", appId);
|
|
|
params.put("sign", sign);
|
|
|
|
|
|
return params;
|
|
|
}
|
|
|
|
|
|
|
|
|
public static String postBody(String url,JSONObject params){
|
|
|
RestTemplate restTemplate = new RestTemplate();
|