5b025fa5f06e0c2488ac6a7055469c3cdda2d090.svn-base 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package com.yihu.platform.test;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import org.apache.http.HttpEntity;
  8. import org.apache.http.NameValuePair;
  9. import org.apache.http.client.entity.UrlEncodedFormEntity;
  10. import org.apache.http.client.methods.CloseableHttpResponse;
  11. import org.apache.http.client.methods.HttpPost;
  12. import org.apache.http.impl.client.CloseableHttpClient;
  13. import org.apache.http.impl.client.HttpClients;
  14. import org.apache.http.message.BasicNameValuePair;
  15. import org.apache.http.util.EntityUtils;
  16. import com.common.json.JSONException;
  17. import com.common.json.JSONObject;
  18. public class Test_WsPlatformPost {
  19. private static final String url = "http://172.18.20.123:8085/WsPlatform/rest";
  20. public static String httpPost(String url, Map<String, String> params) throws IOException {
  21. CloseableHttpClient httpclient = HttpClients.createDefault();
  22. try {
  23. HttpPost httpPost = new HttpPost(url);
  24. if(params!=null&&params.size()>0){
  25. List<NameValuePair> valuePairs = new ArrayList<NameValuePair>(
  26. params.size());
  27. for (Map.Entry<String, String> entry : params.entrySet()) {
  28. NameValuePair nameValuePair = new BasicNameValuePair(
  29. entry.getKey(), String.valueOf(entry.getValue()));
  30. valuePairs.add(nameValuePair);
  31. }
  32. UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(
  33. valuePairs, "UTF-8");
  34. httpPost.setEntity(formEntity);
  35. }
  36. CloseableHttpResponse resp = httpclient.execute(httpPost);
  37. try {
  38. HttpEntity entity = resp.getEntity();
  39. String respContent = EntityUtils.toString(entity, "UTF-8").trim();
  40. return respContent;
  41. } finally {
  42. resp.close();
  43. }
  44. } catch (Exception e) {
  45. e.printStackTrace();
  46. return e.getMessage();
  47. }finally {
  48. httpclient.close();
  49. }
  50. }
  51. public void httpPost(String api, JSONObject paramJSON) {
  52. JSONObject authInfoJSON = new JSONObject();
  53. try {
  54. authInfoJSON.put("ClientId", "");
  55. authInfoJSON.put("ClientVersion", "1.0");
  56. authInfoJSON.put("Sign", "");
  57. authInfoJSON.put("SessionKey", "");
  58. Map<String, String> params = new HashMap<String, String>();
  59. params.put("Api", api);
  60. params.put("Param", paramJSON.toString());
  61. params.put("AuthInfo", authInfoJSON.toString());
  62. params.put("ParamType", "0");
  63. params.put("OutType", "0");
  64. params.put("V", "1.0");
  65. params.put("SequenceNo", Long.toString(System.currentTimeMillis()));
  66. System.out.println(params.toString());
  67. String ret = Test_WsPlatformPost.httpPost(url, params);
  68. System.out.println("结果:" + ret);
  69. } catch (JSONException e) {
  70. e.printStackTrace();
  71. } catch (Exception e) {
  72. e.printStackTrace();
  73. }
  74. }
  75. public static void main(String[] args) {
  76. Test_WsPlatformPost postUtil = new Test_WsPlatformPost();
  77. postUtil.queryDoctorInfoByUid();;
  78. }
  79. /**
  80. * 测试各个功能点
  81. */
  82. //获取accessToken
  83. public void getAccessToken() {
  84. String api = "baseinfo.CommonApi.getAccessToken";
  85. JSONObject json = new JSONObject();
  86. this.httpPost(api, json);
  87. }
  88. public void queryDoctorInfoByUid() {
  89. String api = "baseinfo.DoctorInfoApi.queryDoctorInfoByUid";
  90. JSONObject json = new JSONObject();
  91. try {
  92. json.put("userId", "cd92657f-5b06-11e6-8344-fa163e8aee56");
  93. } catch (JSONException e) {
  94. e.printStackTrace();
  95. }
  96. this.httpPost(api, json);
  97. }
  98. }