原文鏈接:https://blog.csdn.net/justry_deng/article/details/81042379
POST無參:
/**
* POST---無參測試
*
* @date 2018年7月13日 下午4:18:50
*/
@Test
public void doPostTestOne() {
// 獲得Http客戶端(可以理解為:你得先有一個瀏覽器;注意:實際上HttpClient與瀏覽器是不一樣的)
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
// 創建Post請求
HttpPost httpPost = new HttpPost("http://localhost:12345/doPostControllerOne");
// 響應模型
CloseableHttpResponse response = null;
try {
// 由客戶端執行(發送)Post請求
response = httpClient.execute(httpPost);
// 從響應模型中獲取響應實體
HttpEntity responseEntity = response.getEntity();
System.out.println("響應狀態為:" + response.getStatusLine());
if (responseEntity != null) {
System.out.println("響應內容長度為:" + responseEntity.getContentLength());
System.out.println("響應內容為:" + EntityUtils.toString(responseEntity));
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 釋放資源
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
POST有參(普通參數):
注:POST傳遞普通參數時,方式與GET一樣即可,這里以直接在url后綴上參數的方式示例。
/**
* POST---有參測試(普通參數)
*
* @date 2018年7月13日 下午4:18:50
*/
@Test
public void doPostTestFour() {
// 獲得Http客戶端(可以理解為:你得先有一個瀏覽器;注意:實際上HttpClient與瀏覽器是不一樣的)
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
// 參數
StringBuffer params = new StringBuffer();
try {
// 字符數據最好encoding以下;這樣一來,某些特殊字符才能傳過去(如:某人的名字就是“&”,不encoding的話,傳不過去)
params.append("name=" + URLEncoder.encode("&", "utf-8"));
params.append("&");
params.append("age=24");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
// 創建Post請求
HttpPost httpPost = new HttpPost("http://localhost:12345/doPostControllerFour" + "?" + params);
// 設置ContentType(注:如果只是傳普通參數的話,ContentType不一定非要用application/json)
httpPost.setHeader("Content-Type", "application/json;charset=utf8");
// 響應模型
CloseableHttpResponse response = null;
try {
// 由客戶端執行(發送)Post請求
response = httpClient.execute(httpPost);
// 從響應模型中獲取響應實體
HttpEntity responseEntity = response.getEntity();
System.out.println("響應狀態為:" + response.getStatusLine());
if (responseEntity != null) {
System.out.println("響應內容長度為:" + responseEntity.getContentLength());
System.out.println("響應內容為:" + EntityUtils.toString(responseEntity));
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 釋放資源
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
POST有參(對象參數):
先給出User類:

/**
* POST---有參測試(對象參數)
*
* @date 2018年7月13日 下午4:18:50
*/
@Test
public void doPostTestTwo() {
// 獲得Http客戶端(可以理解為:你得先有一個瀏覽器;注意:實際上HttpClient與瀏覽器是不一樣的)
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
// 創建Post請求
HttpPost httpPost = new HttpPost("http://localhost:12345/doPostControllerTwo");
User user = new User();
user.setName("潘曉婷");
user.setAge(18);
user.setGender("女");
user.setMotto("姿勢要優雅~");
// 我這里利用阿里的fastjson,將Object轉換為json字符串;
// (需要導入com.alibaba.fastjson.JSON包)
String jsonString = JSON.toJSONString(user);
StringEntity entity = new StringEntity(jsonString, "UTF-8");
// post請求是將參數放在請求體里面傳過去的;這里將entity放入post請求體中
httpPost.setEntity(entity);
httpPost.setHeader("Content-Type", "application/json;charset=utf8");
// 響應模型
CloseableHttpResponse response = null;
try {
// 由客戶端執行(發送)Post請求
response = httpClient.execute(httpPost);
// 從響應模型中獲取響應實體
HttpEntity responseEntity = response.getEntity();
System.out.println("響應狀態為:" + response.getStatusLine());
if (responseEntity != null) {
System.out.println("響應內容長度為:" + responseEntity.getContentLength());
System.out.println("響應內容為:" + EntityUtils.toString(responseEntity));
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 釋放資源
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
POST有參(普通參數 + 對象參數):
注:POST傳遞普通參數時,方式與GET一樣即可,這里以通過URI獲得HttpPost的方式為例。
先給出User類:

/**
* POST---有參測試(普通參數 + 對象參數)
*
* @date 2018年7月13日 下午4:18:50
*/
@Test
public void doPostTestThree() {
// 獲得Http客戶端(可以理解為:你得先有一個瀏覽器;注意:實際上HttpClient與瀏覽器是不一樣的)
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
// 創建Post請求
// 參數
URI uri = null;
try {
// 將參數放入鍵值對類NameValuePair中,再放入集合中
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("flag", "4"));
params.add(new BasicNameValuePair("meaning", "這是什么鬼?"));
// 設置uri信息,並將參數集合放入uri;
// 注:這里也支持一個鍵值對一個鍵值對地往里面放setParameter(String key, String value)
uri = new URIBuilder().setScheme("http").setHost("localhost").setPort(12345)
.setPath("/doPostControllerThree").setParameters(params).build();
} catch (URISyntaxException e1) {
e1.printStackTrace();
}
HttpPost httpPost = new HttpPost(uri);
// HttpPost httpPost = new
// HttpPost("http://localhost:12345/doPostControllerThree1");
// 創建user參數
User user = new User();
user.setName("潘曉婷");
user.setAge(18);
user.setGender("女");
user.setMotto("姿勢要優雅~");
// 將user對象轉換為json字符串,並放入entity中
StringEntity entity = new StringEntity(JSON.toJSONString(user), "UTF-8");
// post請求是將參數放在請求體里面傳過去的;這里將entity放入post請求體中
httpPost.setEntity(entity);
httpPost.setHeader("Content-Type", "application/json;charset=utf8");
// 響應模型
CloseableHttpResponse response = null;
try {
// 由客戶端執行(發送)Post請求
response = httpClient.execute(httpPost);
// 從響應模型中獲取響應實體
HttpEntity responseEntity = response.getEntity();
System.out.println("響應狀態為:" + response.getStatusLine());
if (responseEntity != null) {
System.out.println("響應內容長度為:" + responseEntity.getContentLength());
System.out.println("響應內容為:" + EntityUtils.toString(responseEntity));
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 釋放資源
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
