123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- package com.yihu.utils;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Map;
- import net.sf.json.JSONObject;
- import org.apache.http.HttpEntity;
- import org.apache.http.NameValuePair;
- import org.apache.http.client.config.RequestConfig;
- import org.apache.http.client.entity.UrlEncodedFormEntity;
- import org.apache.http.client.methods.CloseableHttpResponse;
- 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 com.coreframework.util.AppConfig;
- import com.yihu.utils.aes.MsgCrypt;
- public class OPUtil {
- private static String appId = AppConfig.getValue("AppId"); // 测试通过之后,请替换为健康之路分配的渠道ID
- private static String encodingAesKey = AppConfig.getValue("OpenPlatformSecret");// 测试通过之后,请替换为健康之路分配的秘钥
- private static String serverUrl = AppConfig.getValue("OpenPlatformUrl");// TODO 测试通过之后,请替换为正式线的URL
- public static String callAPI(String apiName, Map<String, String> paramMap) throws Exception {
- String result = null;
- String apiUrl = serverUrl+apiName;
- JSONObject jsonParam =new JSONObject();
- jsonParam.putAll(paramMap);
- String replyMsg=jsonParam.toString();
-
- String timestamp = Long.toString(System.currentTimeMillis());
- MsgCrypt pc = new MsgCrypt(encodingAesKey, appId);
- Map<String,String> mingwen = pc.encryptMsg(replyMsg, timestamp);
- System.out.println("加密后: " + mingwen.toString());
- String encrypt =mingwen.get("encrypt");
- String sign = mingwen.get("sign");
- String result2 = pc.decryptMsg(sign, timestamp, encrypt);
- System.out.println("解密后明文: " + result2);
-
- //post提交数据
- String rep=httpPost(apiUrl,mingwen);
- System.out.println("服务器返回的结果:"+rep);
-
-
- //解密服务器返回的结果
- JSONObject jsonRep=JSONObject.fromObject(rep);
- sign=jsonRep.getString("sign");
- encrypt=jsonRep.getString("encrypt");
- result2 = pc.decryptMsg(sign, timestamp, encrypt);
- System.out.println("服务器返回的结果解密后明文: " + result2);
- return result2;
- }
- public static String httpPost(String url, Map<String, String> params)
- throws IOException {
- CloseableHttpClient httpclient = HttpClients.createDefault();
- try {
- RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(30000).setConnectTimeout(5000).build();//设置请求和传输超时时间
-
- HttpPost httpPost = new HttpPost(url);
- httpPost.setConfig(requestConfig);
- if (params != null && params.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();
- }
- }
- }
|