Skip to content

HttpClient工具类,简单轻松的实现get,post,put和delete请求

Notifications You must be signed in to change notification settings

ArcYu/httpclientutil

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

23 Commits

Repository files navigation

HttpClient工具类

最近在工作中需要在后台调用短信接口,经过研究决定使用HttpClient,自己封装了一个HttpClient工具类,简单轻松的实现get,post,put和delete请求,希望分享给大家。

1. 什么是HttpClient

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

2. 功能介绍

  • 实现了所有 HTTP 的方法(GET,POST,PUT,DELETE 等)
  • 支持自动转向
  • 支持 HTTPS 协议
  • 支持代理服务器等

3. 版本比较

注意本篇博客主要是基于 HttpClient4.5.5 版本的来讲解的,也是现在最新的版本,之所以要提供版本说明的是因为 HttpClient 3 版本和 HttpClient 4 版本差别还是很多大的,基本HttpClient里面的接口都变了,你把 HttpClient 3 版本的代码拿到 HttpClient 4 上面都运行不起来,会报错的。所以一定要注意 HtppClient 的版本问题。

4. HttpClient不能做的事情

HttpClient 不是浏览器,它是一个客户端 HTTP 协议传输类库。HttpClient 被用来发送和接受 HTTP 消息。HttpClient 不会处理 HTTP 消息的内容,不会进行 javascript 解析,不会关心 content type,如果没有明确设置,HttpClient 也不会对请求进行格式化、重定向 url,或者其他任何和 HTTP 消息传输相关的功能。

5. HttpClient使用流程

使用HttpClient发送请求、接收响应很简单,一般需要如下几步即可。

  1. 创建HttpClient对象。
  2. 创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建HttpPost对象。
  3. 如果需要发送请求参数,可调用HttpGetsetParams方法来添加请求参数;对于HttpPost对象而言,可调用setEntity(HttpEntity entity)方法来设置请求参数。
  4. 调用HttpClient对象的execute(HttpUriRequest request)发送请求,该方法返回一个HttpResponse对象。
  5. 调用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可获取服务器的响应头;调用HttpResponse的getEntity()方法可获取HttpEntity对象,该对象包装了服务器的响应内容。程序可通过该对象获取服务器的响应内容。
  6. 释放连接。无论执行方法是否成功,都必须释放连接

6. HttpClient工具类

本项目采用spring-boot构建,话不多说,上代码

6.1 添加依赖

<properties> <httpclient.version>4.5.5</httpclient.version> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> </properties> <dependencies> <!-- springboot的web和test启动库 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!-- apachehttpclient组件 --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> </dependency> </dependencies> <build> <finalName>${project.artifactId}</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!-- 跳过单元测试 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <skipTests>true</skipTests> </configuration> </plugin> </plugins> </build>

6.2 编写spring-boot启动类

/** * Description: springboot启动类 *  * @author JourWon * @date Created on 2018年4月19日 */@SpringBootApplicationpublicclassApplication{publicstaticvoidmain(String[] args){SpringApplication.run(Application.class, args)} }

6.3 编写get和post请求测试controller

/** * Description: get和post请求测试controller *  * @author JourWon * @date Created on 2018年4月19日 */@RestController@RequestMapping("/hello") publicclassHelloWorldController{@GetMapping("/get") publicStringget() throwsInterruptedException{return"get无参请求成功"} @GetMapping("/getWithParam") publicStringgetWithParam(@RequestParamStringmessage){return"get带参请求成功,参数message: " + message} @PostMapping("/post") publicStringpost(@RequestHeader("User-Agent") StringuserAgent, @RequestHeader("Accept") Stringaccept, @RequestHeader("Accept-Language") StringacceptLanguage, @RequestHeader("Accept-Encoding") StringacceptEncoding, @RequestHeader("Cookie") Stringcookie, @RequestHeader("Connection") Stringconn){// 打印请求头信息System.out.println("Cookie = " + cookie); System.out.println("Connection = " + conn); System.out.println("Accept = " + accept); System.out.println("Accept-Language = " + acceptLanguage); System.out.println("Accept-Encoding = " + acceptEncoding); System.out.println("User-Agent = " + userAgent); return"post无参请求成功"} @PostMapping("/postWithParam") publicStringpostWithParam(@RequestParamStringcode, @RequestParamStringmessage){return"post带参请求成功,参数code: " + code + ",参数message: " + message} }

6.4 创建httpClient响应结果对象

/** * Description: 封装httpClient响应结果 *  * @author JourWon * @date Created on 2018年4月19日 */publicclassHttpClientResultimplementsSerializable{/** * 响应状态码 */privateintcode; /** * 响应数据 */privateStringcontent}

6.5 重点,编写httpclient工具类

/** * Description: httpClient工具类 *  * @author JourWon * @date Created on 2018年4月19日 */publicclassHttpClientUtils{// 编码格式。发送编码格式统一用UTF-8privatestaticfinalStringENCODING = "UTF-8"; // 设置连接超时时间,单位毫秒。privatestaticfinalintCONNECT_TIMEOUT = 6000; // 请求获取数据的超时时间(即响应时间),单位毫秒。privatestaticfinalintSOCKET_TIMEOUT = 6000; /** * 发送get请求;不带请求头和请求参数 *  * @param url 请求地址 * @return * @throws Exception */publicstaticHttpClientResultdoGet(Stringurl) throwsException{returndoGet(url, null, null)} /** * 发送get请求;带请求参数 *  * @param url 请求地址 * @param params 请求参数集合 * @return * @throws Exception */publicstaticHttpClientResultdoGet(Stringurl, Map<String, String> params) throwsException{returndoGet(url, null, params)} /** * 发送get请求;带请求头和请求参数 *  * @param url 请求地址 * @param headers 请求头集合 * @param params 请求参数集合 * @return * @throws Exception */publicstaticHttpClientResultdoGet(Stringurl, Map<String, String> headers, Map<String, String> params) throwsException{// 创建httpClient对象CloseableHttpClienthttpClient = HttpClients.createDefault(); // 创建访问的地址URIBuilderuriBuilder = newURIBuilder(url); if (params != null){Set<Entry<String, String>> entrySet = params.entrySet(); for (Entry<String, String> entry : entrySet){uriBuilder.setParameter(entry.getKey(), entry.getValue())} } // 创建http对象HttpGethttpGet = newHttpGet(uriBuilder.build()); /** * setConnectTimeout:设置连接超时时间,单位毫秒。 * setConnectionRequestTimeout:设置从connect Manager(连接池)获取Connection * 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。 * setSocketTimeout:请求获取数据的超时时间(即响应时间),单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。 */RequestConfigrequestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build(); httpGet.setConfig(requestConfig); // 设置请求头packageHeader(headers, httpGet); // 创建httpResponse对象CloseableHttpResponsehttpResponse = null; try{// 执行请求并获得响应结果returngetHttpClientResult(httpResponse, httpClient, httpGet)} finally{// 释放资源release(httpResponse, httpClient)} } /** * 发送post请求;不带请求头和请求参数 *  * @param url 请求地址 * @return * @throws Exception */publicstaticHttpClientResultdoPost(Stringurl) throwsException{returndoPost(url, null, null)} /** * 发送post请求;带请求参数 *  * @param url 请求地址 * @param params 参数集合 * @return * @throws Exception */publicstaticHttpClientResultdoPost(Stringurl, Map<String, String> params) throwsException{returndoPost(url, null, params)} /** * 发送post请求;带请求头和请求参数 *  * @param url 请求地址 * @param headers 请求头集合 * @param params 请求参数集合 * @return * @throws Exception */publicstaticHttpClientResultdoPost(Stringurl, Map<String, String> headers, Map<String, String> params) throwsException{// 创建httpClient对象CloseableHttpClienthttpClient = HttpClients.createDefault(); // 创建http对象HttpPosthttpPost = newHttpPost(url); /** * setConnectTimeout:设置连接超时时间,单位毫秒。 * setConnectionRequestTimeout:设置从connect Manager(连接池)获取Connection * 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。 * setSocketTimeout:请求获取数据的超时时间(即响应时间),单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。 */RequestConfigrequestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build(); httpPost.setConfig(requestConfig); // 设置请求头/*httpPost.setHeader("Cookie", ""); httpPost.setHeader("Connection", "keep-alive"); httpPost.setHeader("Accept", "application/json"); httpPost.setHeader("Accept-Language", "zh-CN,zh;q=0.9"); httpPost.setHeader("Accept-Encoding", "gzip, deflate, br"); httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");*/packageHeader(headers, httpPost); // 封装请求参数packageParam(params, httpPost); // 创建httpResponse对象CloseableHttpResponsehttpResponse = null; try{// 执行请求并获得响应结果returngetHttpClientResult(httpResponse, httpClient, httpPost)} finally{// 释放资源release(httpResponse, httpClient)} } /** * 发送put请求;不带请求参数 *  * @param url 请求地址 * @param params 参数集合 * @return * @throws Exception */publicstaticHttpClientResultdoPut(Stringurl) throwsException{returndoPut(url)} /** * 发送put请求;带请求参数 *  * @param url 请求地址 * @param params 参数集合 * @return * @throws Exception */publicstaticHttpClientResultdoPut(Stringurl, Map<String, String> params) throwsException{CloseableHttpClienthttpClient = HttpClients.createDefault(); HttpPuthttpPut = newHttpPut(url); RequestConfigrequestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build(); httpPut.setConfig(requestConfig); packageParam(params, httpPut); CloseableHttpResponsehttpResponse = null; try{returngetHttpClientResult(httpResponse, httpClient, httpPut)} finally{release(httpResponse, httpClient)} } /** * 发送delete请求;不带请求参数 *  * @param url 请求地址 * @param params 参数集合 * @return * @throws Exception */publicstaticHttpClientResultdoDelete(Stringurl) throwsException{CloseableHttpClienthttpClient = HttpClients.createDefault(); HttpDeletehttpDelete = newHttpDelete(url); RequestConfigrequestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build(); httpDelete.setConfig(requestConfig); CloseableHttpResponsehttpResponse = null; try{returngetHttpClientResult(httpResponse, httpClient, httpDelete)} finally{release(httpResponse, httpClient)} } /** * 发送delete请求;带请求参数 *  * @param url 请求地址 * @param params 参数集合 * @return * @throws Exception */publicstaticHttpClientResultdoDelete(Stringurl, Map<String, String> params) throwsException{if (params == null){params = newHashMap<String, String>()} params.put("_method", "delete"); returndoPost(url, params)} /** * Description: 封装请求头 * @param params * @param httpMethod */publicstaticvoidpackageHeader(Map<String, String> params, HttpRequestBasehttpMethod){// 封装请求头if (params != null){Set<Entry<String, String>> entrySet = params.entrySet(); for (Entry<String, String> entry : entrySet){// 设置到请求头到HttpRequestBase对象中httpMethod.setHeader(entry.getKey(), entry.getValue())} } } /** * Description: 封装请求参数 *  * @param params * @param httpMethod * @throws UnsupportedEncodingException */publicstaticvoidpackageParam(Map<String, String> params, HttpEntityEnclosingRequestBasehttpMethod) throwsUnsupportedEncodingException{// 封装请求参数if (params != null){List<NameValuePair> nvps = newArrayList<NameValuePair>(); Set<Entry<String, String>> entrySet = params.entrySet(); for (Entry<String, String> entry : entrySet){nvps.add(newBasicNameValuePair(entry.getKey(), entry.getValue()))} // 设置到请求的http对象中httpMethod.setEntity(newUrlEncodedFormEntity(nvps, ENCODING))} } /** * Description: 获得响应结果 *  * @param httpResponse * @param httpClient * @param httpMethod * @return * @throws Exception */publicstaticHttpClientResultgetHttpClientResult(CloseableHttpResponsehttpResponse, CloseableHttpClienthttpClient, HttpRequestBasehttpMethod) throwsException{// 执行请求httpResponse = httpClient.execute(httpMethod); // 获取返回结果if (httpResponse != null && httpResponse.getStatusLine() != null){Stringcontent = ""; if (httpResponse.getEntity() != null){content = EntityUtils.toString(httpResponse.getEntity(), ENCODING)} returnnewHttpClientResult(httpResponse.getStatusLine().getStatusCode(), content)} returnnewHttpClientResult(HttpStatus.SC_INTERNAL_SERVER_ERROR)} /** * Description: 释放资源 *  * @param httpResponse * @param httpClient * @throws IOException */publicstaticvoidrelease(CloseableHttpResponsehttpResponse, CloseableHttpClienthttpClient) throwsIOException{// 释放资源if (httpResponse != null){httpResponse.close()} if (httpClient != null){httpClient.close()} } }

6.6 启动spring-boot,测试get、post请求

/** * Description: HttpClientUtils工具类测试 *  * @author JourWon * @date Created on 2018年4月19日 */publicclassHttpClientUtilsTest{/** * Description: 测试get无参请求 *  * @throws Exception */@TestpublicvoidtestGet() throwsException{HttpClientResultresult = HttpClientUtils.doGet("http://127.0.0.1:8080/hello/get"); System.out.println(result)} /** * Description: 测试get带参请求 *  * @throws Exception */@TestpublicvoidtestGetWithParam() throwsException{Map<String, String> params = newHashMap<String, String>(); params.put("message", "helloworld"); HttpClientResultresult = HttpClientUtils.doGet("http://127.0.0.1:8080/hello/getWithParam", params); System.out.println(result)} /** * Description: 测试post带请求头不带请求参数 *  * @throws Exception */@TestpublicvoidtestPost() throwsException{Map<String, String> headers = newHashMap<String, String>(); headers.put("Cookie", "123"); headers.put("Connection", "keep-alive"); headers.put("Accept", "application/json"); headers.put("Accept-Language", "zh-CN,zh;q=0.9"); headers.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"); HttpClientResultresult = HttpClientUtils.doPost("http://127.0.0.1:8080/hello/post", headers, null); System.out.println(result)} /** * Description: 测试post带参请求 *  * @throws Exception */@TestpublicvoidtestPostWithParam() throwsException{Map<String, String> params = newHashMap<String, String>(); params.put("code", "0"); params.put("message", "helloworld"); HttpClientResultresult = HttpClientUtils.doPost("http://127.0.0.1:8080/hello/postWithParam", params); System.out.println(result)} }

完整代码下载链接

About

HttpClient工具类,简单轻松的实现get,post,put和delete请求

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java100.0%