首页 > 代码库 > httpclient之入门使用

httpclient之入门使用

1.简介

          HTTP 协议可能是现在 Internet 上使用得最多、最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源。虽然在 JDK 的 java net包中已经提供了访问 HTTP 协议的基本功能,但是对于大部分应用程序来说,JDK 库本身提供的功能还不够丰富和灵活。HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。

2.使用场景

           HttpClient可以灵活地访问网络资源,他不仅可以灵活地在客户端访问服务端的网络资源。在服务端,也可以通过HttpClient完成跨层次访问。

3.使用

         下面可以通过HttpClientUtil清晰的掌握HttpClient的基本使用方法:(GET,POST和JSON的提交)

  

  1 public class HttpClientUtil {
  2 
  3     public static String doGet(String url, Map<String, String> param) {
  4 
  5         // 创建Httpclient对象
  6         CloseableHttpClient httpclient = HttpClients.createDefault();
  7 
  8         String resultString = "";
  9         CloseableHttpResponse response = null;
 10         try {
 11             // 创建uri
 12             URIBuilder builder = new URIBuilder(url);
 13             if (param != null) {
 14                 //拼接请求参数
 15                 for (String key : param.keySet()) {
 16                     builder.addParameter(key, param.get(key));
 17                 }
 18             }
 19             URI uri = builder.build();
 20 
 21             // 创建http GET请求
 22             HttpGet httpGet = new HttpGet(uri);
 23 
 24             // 执行请求
 25             response = httpclient.execute(httpGet);
 26             // 判断返回状态是否为200
 27             if (response.getStatusLine().getStatusCode() == 200) {
 28                 resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
 29             }
 30         } catch (Exception e) {
 31             e.printStackTrace();
 32         } finally {
 33             try {
 34                 if (response != null) {
 35                     response.close();
 36                 }
 37                 httpclient.close();
 38             } catch (IOException e) {
 39                 e.printStackTrace();
 40             }
 41         }
 42         return resultString;
 43     }
 44 
 45     public static String doGet(String url) {
 46         return doGet(url, null);
 47     }
 48 
 49     public static String doPost(String url, Map<String, String> param) {
 50         // 创建Httpclient对象
 51         CloseableHttpClient httpClient = HttpClients.createDefault();
 52         //创建HttpResponse响应
 53         CloseableHttpResponse response = null;
 54         String resultString = "";
 55         try {
 56             // 创建Http Post请求
 57             HttpPost httpPost = new HttpPost(url);
 58             // 创建参数列表
 59             if (param != null) {
 60                 //拼接表单参数
 61                 List<NameValuePair> paramList = new ArrayList<>();
 62                 for (String key : param.keySet()) {
 63                     paramList.add(new BasicNameValuePair(key, param.get(key)));
 64                 }
 65                 // 模拟表单
 66                 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);
 67                 httpPost.setEntity(entity);
 68             }
 69             // 执行http请求
 70             response = httpClient.execute(httpPost);
 71             resultString = EntityUtils.toString(response.getEntity(), "utf-8");
 72         } catch (Exception e) {
 73             e.printStackTrace();
 74         } finally {
 75             try {
 76                 response.close();
 77             } catch (IOException e) {
 78                 // TODO Auto-generated catch block
 79                 e.printStackTrace();
 80             }
 81         }
 82 
 83         return resultString;
 84     }
 85 
 86     public static String doPost(String url) {
 87         return doPost(url, null);
 88     }
 89     
 90     public static String doPostJson(String url, String json) {
 91         // 创建Httpclient对象
 92         CloseableHttpClient httpClient = HttpClients.createDefault();
 93         CloseableHttpResponse response = null;
 94         String resultString = "";
 95         try {
 96             // 创建Http Post请求
 97             HttpPost httpPost = new HttpPost(url);
 98             // 创建请求内容
 99             StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
100             //设置Post请求
101             httpPost.setEntity(entity);
102             // 执行http请求
103             response = httpClient.execute(httpPost);
104             resultString = EntityUtils.toString(response.getEntity(), "utf-8");
105         } catch (Exception e) {
106             e.printStackTrace();
107         } finally {
108             try {
109                 response.close();
110             } catch (IOException e) {
111                 // TODO Auto-generated catch block
112                 e.printStackTrace();
113             }
114         }
115 
116         return resultString;
117     }
118 }

 

httpclient之入门使用