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

HttpClient4.x工具获取如何使用

时间:2019-01-01 22:44:49      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:response   instance   ext   enable   efault   tput   数据   tcl   com   

HttpClient4.x工具可以让我们输入url,就可以请求某个页面(个人感觉挺实用的,特别是封装在代码中)

首先我们需要在maven工程中添加依赖

<dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
                <version>4.5.3</version>
            </dependency>
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpmime</artifactId>
                <version>4.5.2</version>
            </dependency>
 
            <dependency>
                <groupId>commons-codec</groupId>
                <artifactId>commons-codec</artifactId>
            </dependency>
            <dependency>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
                <version>1.1.1</version>
            </dependency>
                    <dependency>
                    <groupId>org.apache.httpcomponents</groupId>
                    <artifactId>httpcore</artifactId>
            </dependency>
 
 
        <!-- gson工具,封装http的时候使用 -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.0</version>
        </dependency>
 
接下来,我们来封装一下,有get/post请求
 1 /**
 2 * 封装http get post
 3 */
 4 public class HttpUtils {
 5 
 6 private static final Gson gson = new Gson();
 7 /**
 8 * get方法
 9 */
10 public static Map<String,Object> doget(String url,int timeout){
11 Map<String,Object> map = new HashMap<>();
12 CloseableHttpClient httpClient = HttpClients.createDefault();
  //配置可有可无,根据个人情景
13 RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeout) //设置连接超时时间 14 .setConnectionRequestTimeout(timeout) //设置请求超时时间 15 .setSocketTimeout(timeout) 16 .setRedirectsEnabled(true) //设置允许自动重定向 17 .build(); 1819 HttpGet httpGet = new HttpGet(url); 20 httpGet.setConfig(requestConfig); 21 22 try { 23 HttpResponse httpResponse = httpClient.execute(httpGet); 24 if(httpResponse.getStatusLine().getStatusCode() == 200){ 25 String jsonResult = EntityUtils.toString(httpResponse.getEntity()); 26 map = gson.fromJson(jsonResult, map.getClass()); //通过gson工具,将响应流转换成map集合 27 } 28 29 }catch(Exception e){ 30 e.printStackTrace(); 31 }finally{ 32 try{ 33 httpClient.close(); 34 }catch(Exception e) { 35 e.printStackTrace(); 36 } 37 } 38 return map; 39 } 40 /** 41 * 封装post,与get有异曲同工之妙 42 */ 43 public static String dopost(String url,String data,int timeout){ 44 CloseableHttpClient httpClient = HttpClients.createDefault(); 45 RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeout) //设置连接超时 46 .setConnectionRequestTimeout(timeout) //设置请求超时 47 .setRedirectsEnabled(true) //设置允许自动重定向 48 .build(); 49 50 HttpPost httpPost = new HttpPost(url); 51 httpPost.setConfig(requestConfig); 52 httpPost.addHeader("Content-Type","text/html; chartset=UTF-8"); //添加头信息 53 if (data != null && data instanceof String) {//使用字符串传参 54 StringEntity stringEntity = new StringEntity(data, "UTF-8"); 55 httpPost.setEntity(stringEntity); 56 } 57 try { 58 CloseableHttpResponse httpResponse = httpClient.execute(httpPost); 59 HttpEntity httpEntity = httpResponse.getEntity(); 60 if (httpResponse.getStatusLine().getStatusCode() == 200){ 61 String result = EntityUtils.toString(httpEntity); 62 return result; 63 } 64 } catch (Exception e) { 65 e.printStackTrace(); 66 }finally { 67 try{ 68 httpClient.close(); 69 }catch (Exception e){ 70 e.printStackTrace(); 71 } 72 } 73 74 return null; 75 76 } 77 }

封装好HttpClient工具后,我们来调用一下

get请求

HttpUtils.doget(请求地址,请求时间(timeout));

post请求

HttpUtils.dopost(请求地址, 请求传的数据(传之前需要转换成string), 请求时间(timeout));

 

HttpClient4.x工具获取如何使用

标签:response   instance   ext   enable   efault   tput   数据   tcl   com   

原文地址:https://www.cnblogs.com/zexin/p/10206266.html

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