/*
* HttpClient Post提交 带参数
*/
@Test
public void fun4() throws ClientProtocolException, IOException{
//1、创建HttpClient
CloseableHttpClient clients = HttpClients.createDefault();
//2、封装请求参数
List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
list.add(new BasicNameValuePair("username", "cgx"));
list.add(new BasicNameValuePair("password", "123456"));
//3、转化参数
UrlEncodedFormEntity refe = new UrlEncodedFormEntity(list,Consts.UTF_8);
//4、创建HttpPost
HttpPost post = new HttpPost("http://localhost:8080/itcast297/loginAction_login");
//5、设置参数
post.setEntity(refe);
//3、执行请求
CloseableHttpResponse response = clients.execute(post);
//4获取实体
HttpEntity entity = response.getEntity();
//将实体转换成字符串
System.out.println(EntityUtils.toString(entity));
response.close();
}
HttpClient Post提交 带参数
最新推荐文章于 2024-02-18 14:44:39 发布