HttpClient常用方法总结

1、HttpPost发送表单请求

1  String url = "";
2  HttpPost httpPost = new HttpPost(url);
3  List<NameValuePair> params = new ArrayList<>();
4  params.add(new BasicNameValuePair("username", "root"));
5  params.add(new BasicNameValuePair("password", "123456"));
6  UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(params, "utf-8");
7  httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
8  httpPost.setEntity(uefEntity);

 2、HttpPost发送json参数请求

1 String url = "";
2 String json = ""; // 请求参数,json格式
3 HttpPost httpPost = new HttpPost(url);
4 StringEntity entity = new StringEntity(json, "UTF-8");
5 httpPost.setHeader("Content-Type", "application/json");
6 httpPost.setEntity(entity);

 3、HttpPost发送xml参数请求

1 String url = "";
2 String xml = ""; // 请求参数,xml格式
3 HttpPost httpPost = new HttpPost(url);
4 StringEntity entity = new StringEntity(xml, "UTF-8");
5 httpPost.setHeader("Content-Type", "text/xml");
6 httpPost.setEntity(entity);

 

posted @ 2019-11-15 15:55  yixiu868  阅读(440)  评论(0编辑  收藏  举报