接口自动化测试HttpClient-基础篇2-Post请求

HttpClient Post请求及响应处理,大致和Get请求相同

//创建HttpClient对象

  CloseableHttpClient httpclient = HttpClients.createDefault();

// 创建带请求地址的HttpPost对象

  HttpPost httpPost = new HttpPost("http://***");

//设置HttpPost参数

  StringEntity entity = new StringEntity("param","utf-8");

  entity.setContentType("application/json");

  httpPost.setEntity(entity);

================

//执行获取post请求的响应

  CloseableHttpResponse response = httpclient.execute(httpPost);

// 获取响应实体

  HttpEntity entity = response.getEntity();

// 获取响应内容

  EntityUtils.toString(entity, "UTF-8"));

//释放资源

  EntityUtils.consume(entity);

实例:demo

原文地址:https://www.cnblogs.com/cocowang68/p/9105479.html