123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- package com.yihu.platform.test;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import org.apache.http.HttpEntity;
- import org.apache.http.NameValuePair;
- 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.common.json.JSONException;
- import com.common.json.JSONObject;
- public class Test_WsPlatformPost {
- private static final String url = "http://172.18.20.123:8085/WsPlatform/rest";
-
- public static String httpPost(String url, Map<String, String> params) throws IOException {
- 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 e.getMessage();
- }finally {
- httpclient.close();
- }
- }
-
- public void httpPost(String api, JSONObject paramJSON) {
- JSONObject authInfoJSON = new JSONObject();
- try {
- authInfoJSON.put("ClientId", "");
- authInfoJSON.put("ClientVersion", "1.0");
- authInfoJSON.put("Sign", "");
- authInfoJSON.put("SessionKey", "");
- Map<String, String> params = new HashMap<String, String>();
- params.put("Api", api);
- params.put("Param", paramJSON.toString());
- params.put("AuthInfo", authInfoJSON.toString());
- params.put("ParamType", "0");
- params.put("OutType", "0");
- params.put("V", "1.0");
- params.put("SequenceNo", Long.toString(System.currentTimeMillis()));
- System.out.println(params.toString());
- String ret = Test_WsPlatformPost.httpPost(url, params);
- System.out.println("结果:" + ret);
- } catch (JSONException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- Test_WsPlatformPost postUtil = new Test_WsPlatformPost();
- postUtil.queryDoctorInfoByUid();;
- }
-
- /**
- * 测试各个功能点
- */
- //获取accessToken
- public void getAccessToken() {
- String api = "baseinfo.CommonApi.getAccessToken";
- JSONObject json = new JSONObject();
- this.httpPost(api, json);
- }
-
- public void queryDoctorInfoByUid() {
- String api = "baseinfo.DoctorInfoApi.queryDoctorInfoByUid";
- JSONObject json = new JSONObject();
- try {
- json.put("userId", "cd92657f-5b06-11e6-8344-fa163e8aee56");
- } catch (JSONException e) {
- e.printStackTrace();
- }
- this.httpPost(api, json);
- }
- }
|