public void requestByPostMethod(){
CloseableHttpClient httpClient = getHttpClient();
try {
HttpPost post = new HttpPost("http://cloudapi.dezhi.com/usewr");
//創建參數列表
List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
list.add(new BasicNameValuePair("commandid", "register"));
list.add(new BasicNameValuePair("agency_id", "7"));
list.add(new BasicNameValuePair("user_id", "5211314"));
list.add(new BasicNameValuePair("user_name", "tyll"));
list.add(new BasicNameValuePair("hash", "998dcafee9be22ec37b03f87828f35f9"));
//url格式編碼
UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(list,"UTF-8");
post.setEntity(uefEntity);
HttpEntity entity2 = post.getEntity();
System.out.println("====================="+EntityUtils.toString(entity2, "utf-8")); // 獲取網頁內容
System.out.println("POST 請求...." + post.getURI());
//執行請求
CloseableHttpResponse httpResponse = httpClient.execute(post);
try{
HttpEntity entity = httpResponse.getEntity();
if (null != entity){
System.out.println("-------------------------------------------------------");
System.out.println(EntityUtils.toString(uefEntity));
System.out.println("-------------------------------------------------------");
}
} finally{
httpResponse.close();
}
} catch( UnsupportedEncodingException e){
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
finally{
try{
closeHttpClient(httpClient);
} catch(Exception e){
e.printStackTrace();
}
}
}
private CloseableHttpClient getHttpClient(){
return HttpClients.createDefault();
}
private void closeHttpClient(CloseableHttpClient client) throws IOException{
if (client != null){
client.close();
}
}