原文链接: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();
}
}
}

  

  

最新文章

  1. fix orphaned user
  2. java中Integer,String判断相等与integer的比较大小
  3. .NET 农码一生
  4. exit
  5. android.util.TypedValue.applyDimension
  6. mvc通过ActionFilterAttribute做登录检查
  7. 高性能Server---Reactor模型-----Socket
  8. vi--文本编辑常用快捷键之光标移动
  9. css3中的动画处理
  10. Netty启动分析
  11. dede调用时间大全标签,不同格式!
  12. hdu 4963(中途相遇法)
  13. 构建安全的Xml Web Service系列之如何察看SoapMessage
  14. matlab笔记(1) 元胞结构cell2mat和num2cell
  15. (文件)图片上传,Spring或SpringMVC框架
  16. String 转化成java.sql.Date和java.sql.Time
  17. PHP 购物车 php闭包 array_walk
  18. XML数据读取——Digester简单使用
  19. BZOJ5177 : [Jsoi2013]贪心的导游
  20. 没有 iOS 开发者账号的情况下部署到真机的方法

热门文章

  1. 51Nod - 1255
  2. 愉快地使用Windows Terminal
  3. 通过adb调试Unity开发的安卓应用
  4. 线程的 run() 和 start() ; 太骚了 ~~
  5. OpenStack的Heat组件详解
  6. 2.Redis安装和简单使用
  7. Android_适配器(adapter)之ArrayAdapter
  8. [C#打包部署教程]001.VS2012 + InstallShield
  9. Java IO(十九)PrintStream 和 PrintWriter
  10. 【补充说明】Gauge框架在JS中的简单应用