HttpClientUtil 工具类
发布日期:2021-06-30 14:54:12 浏览次数:2 分类:技术文章

本文共 5937 字,大约阅读时间需要 19 分钟。

HttpClientUtil 工具类

依赖

org.apache.httpcomponents
httpclient
4.5.2

工具类

package com.jerry.utls;package com.httpclinet;import java.io.IOException;import java.util.ArrayList;import java.util.List;import java.util.Map;import org.apache.http.HttpEntity;import org.apache.http.NameValuePair;import org.apache.http.client.config.RequestConfig;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.StringEntity;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.message.BasicNameValuePair;import org.apache.http.util.EntityUtils;/** * http工具类 * @author jerryjin */public class HttpClientUtil {
private RequestConfig requestConfig = RequestConfig.custom() .setSocketTimeout(15000) .setConnectTimeout(15000) .setConnectionRequestTimeout(15000) .build(); private volatile static HttpClientUtil instance = null; private HttpClientUtil(){
} public static HttpClientUtil getInstance(){
if (instance == null) {
synchronized (HttpClientUtil.class) {
if (instance == null) {
instance = new HttpClientUtil(); } } } return instance; } /** * 发送 post请求 * @param httpUrl 地址 */ public String sendHttpPost(String httpUrl) {
HttpPost httpPost = new HttpPost(httpUrl); return sendHttpPost(httpPost); } /** * 发送 post请求 * @param httpUrl 地址 * @param params 参数(格式:key1=value1&key2=value2) */ public String sendHttpPost(String httpUrl, String params) {
HttpPost httpPost = new HttpPost(httpUrl); try {
//设置参数 StringEntity stringEntity = new StringEntity(params, "UTF-8"); stringEntity.setContentType("application/x-www-form-urlencoded"); httpPost.setEntity(stringEntity); } catch (Exception e) {
e.printStackTrace(); } return sendHttpPost(httpPost); } /** * 发送 post请求 * @param httpUrl 地址 * @param maps 参数 */ public String sendHttpPost(String httpUrl, Map
maps) {
HttpPost httpPost = new HttpPost(httpUrl); // 创建参数队列 List
nameValuePairs = new ArrayList
(); for (String key : maps.keySet()) {
nameValuePairs.add(new BasicNameValuePair(key, maps.get(key))); } try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8")); } catch (Exception e) {
e.printStackTrace(); } return sendHttpPost(httpPost); } /** * 发送Post请求 * @param httpPost * @return */ private String sendHttpPost(HttpPost httpPost) {
CloseableHttpClient httpClient = null; CloseableHttpResponse response = null; HttpEntity entity = null; String responseContent = null; try {
// 创建默认的httpClient实例. httpClient = HttpClients.createDefault(); httpPost.setConfig(requestConfig); // 执行请求 response = httpClient.execute(httpPost); entity = response.getEntity(); responseContent = EntityUtils.toString(entity, "UTF-8"); } catch (Exception e) {
e.printStackTrace(); } finally {
try {
// 关闭连接,释放资源 if (response != null) {
response.close(); } if (httpClient != null) {
httpClient.close(); } } catch (IOException e) {
e.printStackTrace(); } } return responseContent; } /** * 发送 get请求 * @param httpUrl */ public String sendHttpGet(String httpUrl) {
HttpGet httpGet = new HttpGet(httpUrl); return sendHttpGet(httpGet); } /** * 发送Get请求 * @param httpGet * @return */ public String sendHttpGet(HttpGet httpGet) {
CloseableHttpClient httpClient = null; CloseableHttpResponse response = null; HttpEntity entity = null; String responseContent = null; try {
// 创建默认的httpClient实例. httpClient = HttpClients.createDefault(); httpGet.setConfig(requestConfig); // 执行请求 response = httpClient.execute(httpGet); entity = response.getEntity(); responseContent = EntityUtils.toString(entity, "UTF-8"); } catch (Exception e) {
e.printStackTrace(); } finally {
try {
// 关闭连接,释放资源 if (response != null) {
response.close(); } if (httpClient != null) {
httpClient.close(); } } catch (IOException e) {
e.printStackTrace(); } } return responseContent; }}

客户端

import org.apache.http.client.methods.HttpGet;/** * @author jerryjin * @Classname HttpClinetDemo * @Date 2021-04-11 12:36 */public class HttpClinetDemo {
public static void main(String[] args) throws CloneNotSupportedException {
String url = "https://jerryjin.blog.csdn.net/article/details/115766780"; HttpGet get = new HttpGet(url); get.addHeader("User-Agent", " Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"); String s = HttpClientUtil.getInstance().sendHttpGet(get); System.out.println(s); }}

转载地址:https://jerryjin.blog.csdn.net/article/details/115766780 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:设计模式 - 学习笔记 - 单例模式Singleton Pattern
下一篇:微信扫码打开APP

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年05月01日 04时47分49秒