分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

HttpClient4.x工具获取如何使用

发布时间:2023-09-06 02:28责任编辑:苏小强关键词:暂无标签

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工具获取如何使用

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

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved