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 params) throws IOException { CloseableHttpClient httpclient = HttpClients.createDefault(); try { HttpPost httpPost = new HttpPost(url); if(params!=null&¶ms.size()>0){ List valuePairs = new ArrayList( params.size()); for (Map.Entry 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 params = new HashMap(); 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); } }