HttpClient使用

HttpClient使用

注意请求参数!!!

// 创建HttpClient模板
HttpClient client = new DefaultHttpClient();  
//创建post模板
HttpPost post = new HttpPost("http://cnivi.com.cn/login");
//创建json模板
List<NameValuePair> formparams = new ArrayList<NameValuePair>();  
//添加json内容
formparams.add(new BasicNameValuePair("account", ""));  
formparams.add(new BasicNameValuePair("password", ""));
//创建entity体
HttpEntity reqEntity = new UrlEncodedFormEntity(formparams, "utf-8");
//执行post请求
HttpResponse response = client.execute(post);
//打印响应体
HttpEntity resEntity = response.getEntity();  
String message = EntityUtils.toString(resEntity, "utf-8");  
System.out.println(message);
posted @ 2021-04-02 09:50  王居三木  阅读(122)  评论(0编辑  收藏  举报