码迷,mamicode.com
首页 > Web开发 > 详细

HttpClient 用于解决测试时候乱码的问题

时间:2017-05-28 09:49:00      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:log   分享   cep   com   asi   pos   res   app   iat   

    

@Test

public void doPostWithParam() throws Exception, IOException {

CloseableHttpClient httpClient = HttpClients.createDefault();

// 创建一个post对象
HttpPost post = new HttpPost("http://localhost:8082/httpclient/post.do");

// 创建一个entity,模拟一个表单
List<NameValuePair> kvList = new ArrayList<>();

kvList.add(new BasicNameValuePair("username", "张三"));
kvList.add(new BasicNameValuePair("password", "123"));

// 包装成一个entity对象

StringEntity entity = new UrlEncodedFormEntity(kvList);

// 设置请求的内容

post.setEntity(entity);

// 执行post请求
CloseableHttpResponse respose = httpClient.execute(post);

String string = EntityUtils.toString(respose.getEntity());
System.out.println(string);

respose.close();
httpClient.close();
}

 

 

在controller中设置如下内容

 

 

@RequestMapping(value = "/httpclient/post",method=RequestMethod.POST,
produces=MediaType.TEXT_PLAIN_VALUE+";charset=utf-8")
@ResponseBody

public String testPost(String username,String password){

System.out.println("username" + username + "/t password" + password) ;

//return TaotaoResult.ok();

return "username" + username + "/t password" + password ;
}
}

 

 

 

技术分享技术分享技术分享

HttpClient 用于解决测试时候乱码的问题

标签:log   分享   cep   com   asi   pos   res   app   iat   

原文地址:http://www.cnblogs.com/xiaohouzai/p/6914850.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!