f22295becb55589bdf742350fd940e3a2b9352a0.svn-base 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package com.yihu.utils;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.List;
  6. import java.util.Map;
  7. import net.sf.json.JSONObject;
  8. import org.apache.http.HttpEntity;
  9. import org.apache.http.NameValuePair;
  10. import org.apache.http.client.config.RequestConfig;
  11. import org.apache.http.client.entity.UrlEncodedFormEntity;
  12. import org.apache.http.client.methods.CloseableHttpResponse;
  13. import org.apache.http.client.methods.HttpPost;
  14. import org.apache.http.impl.client.CloseableHttpClient;
  15. import org.apache.http.impl.client.HttpClients;
  16. import org.apache.http.message.BasicNameValuePair;
  17. import org.apache.http.util.EntityUtils;
  18. import com.coreframework.util.AppConfig;
  19. import com.yihu.utils.aes.MsgCrypt;
  20. public class OPUtil {
  21. private static String appId = AppConfig.getValue("AppId"); // 测试通过之后,请替换为健康之路分配的渠道ID
  22. private static String encodingAesKey = AppConfig.getValue("OpenPlatformSecret");// 测试通过之后,请替换为健康之路分配的秘钥
  23. private static String serverUrl = AppConfig.getValue("OpenPlatformUrl");// TODO 测试通过之后,请替换为正式线的URL
  24. public static String callAPI(String apiName, Map<String, String> paramMap) throws Exception {
  25. String result = null;
  26. String apiUrl = serverUrl+apiName;
  27. JSONObject jsonParam =new JSONObject();
  28. jsonParam.putAll(paramMap);
  29. String replyMsg=jsonParam.toString();
  30. String timestamp = Long.toString(System.currentTimeMillis());
  31. MsgCrypt pc = new MsgCrypt(encodingAesKey, appId);
  32. Map<String,String> mingwen = pc.encryptMsg(replyMsg, timestamp);
  33. System.out.println("加密后: " + mingwen.toString());
  34. String encrypt =mingwen.get("encrypt");
  35. String sign = mingwen.get("sign");
  36. String result2 = pc.decryptMsg(sign, timestamp, encrypt);
  37. System.out.println("解密后明文: " + result2);
  38. //post提交数据
  39. String rep=httpPost(apiUrl,mingwen);
  40. System.out.println("服务器返回的结果:"+rep);
  41. //解密服务器返回的结果
  42. JSONObject jsonRep=JSONObject.fromObject(rep);
  43. sign=jsonRep.getString("sign");
  44. encrypt=jsonRep.getString("encrypt");
  45. result2 = pc.decryptMsg(sign, timestamp, encrypt);
  46. System.out.println("服务器返回的结果解密后明文: " + result2);
  47. return result2;
  48. }
  49. public static String httpPost(String url, Map<String, String> params)
  50. throws IOException {
  51. CloseableHttpClient httpclient = HttpClients.createDefault();
  52. try {
  53. RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(30000).setConnectTimeout(5000).build();//设置请求和传输超时时间
  54. HttpPost httpPost = new HttpPost(url);
  55. httpPost.setConfig(requestConfig);
  56. if (params != null && params.size() > 0) {
  57. List<NameValuePair> valuePairs = new ArrayList<NameValuePair>(
  58. params.size());
  59. for (Map.Entry<String, String> entry : params.entrySet()) {
  60. NameValuePair nameValuePair = new BasicNameValuePair(
  61. entry.getKey(), String.valueOf(entry.getValue()));
  62. valuePairs.add(nameValuePair);
  63. }
  64. UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(
  65. valuePairs, "UTF-8");
  66. httpPost.setEntity(formEntity);
  67. }
  68. CloseableHttpResponse resp = httpclient.execute(httpPost);
  69. try {
  70. HttpEntity entity = resp.getEntity();
  71. String respContent = EntityUtils.toString(entity, "UTF-8")
  72. .trim();
  73. return respContent;
  74. } finally {
  75. resp.close();
  76. }
  77. } catch (Exception e) {
  78. e.printStackTrace();
  79. return null;
  80. } finally {
  81. httpclient.close();
  82. }
  83. }
  84. }