• HttpClient封装工具类


    package com.example.demo.util;
    
    import org.apache.http.client.config.RequestConfig;
    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.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
    import org.apache.http.util.EntityUtils;
    import org.springframework.stereotype.Component;
    
    import java.io.IOException;
    
    /**
     * @author lyd
     * @Description:
     * @date 17:21
     */
    @Component
    public class HttpClientUtils {
    
    	private PoolingHttpClientConnectionManager poolingHttpClientConnectionManager;
    
    	public HttpClientUtils() {
    		this.poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager();
    
    		//    设置最大连接数
    		poolingHttpClientConnectionManager.setMaxTotal(200);
    		//    设置每个主机的并发数
    		poolingHttpClientConnectionManager.setDefaultMaxPerRoute(20);
    
    	}
    
    	/**
    	 * 设置请求信息
    	 *
    	 * @return
    	 */
    	private RequestConfig getConfig() {
    		// 配置信息
    		RequestConfig requestConfig = RequestConfig.custom()
    				// 设置连接超时时间
    				.setConnectTimeout(5000)
    				// 设置请求超时时间
    				.setConnectionRequestTimeout(5000)
    				// 设置读写超时时间
    				.setSocketTimeout(5000)
    				// 设置是否允许重定向
    				.setRedirectsEnabled(true).build();
    		return requestConfig;
    	}
    
    	/**
    	 * get请求
    	 *
    	 * @param url
    	 * @return
    	 */
    	public String doGet(String url) {
    		// 获取HttpClient对象
    		CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(poolingHttpClientConnectionManager).build();
    
    		// 创建爱你httpGet请求,设置url地址
    		HttpGet httpGet = new HttpGet(url);
    		// 设置请求信息
    		httpGet.setConfig(getConfig());
    
    		// 使用HttpClient发起请求,获取响应
    		CloseableHttpResponse closeableHttpResponse = null;
    		try {
    			closeableHttpResponse = httpClient.execute(httpGet);
    
    			// 解析响应,返回结果
    			if (closeableHttpResponse.getStatusLine().getStatusCode() == 200) {
    				// 判断响应体是否不为空
    				if (closeableHttpResponse.getEntity() != null) {
    					String content = EntityUtils.toString(closeableHttpResponse.getEntity(), "UTF-8");
    					return content;
    				}
    
    			}
    		} catch (IOException e) {
    			e.printStackTrace();
    		} finally {
    			try {
    				if (closeableHttpResponse != null) {
    					// 关闭连接
    					closeableHttpResponse.close();
    				}
    				// 不能关闭,现在使用的是连接管理器
    				// httpClient.close();
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    
    		}
    		return null;
    
    	}
    
    
    	/**
    	 * post请求
    	 *
    	 * @param url
    	 * @return
    	 */
    	public String doPost(String url) {
    		// 获取HttpClient对象
    		CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(poolingHttpClientConnectionManager).build();
    
    		// 创建爱你HttpPost请求,设置url地址
    		HttpPost httpPost = new HttpPost(url);
    		// 设置请求信息
    		httpPost.setConfig(getConfig());
    
    		// 使用HttpClient发起请求,获取响应
    		CloseableHttpResponse closeableHttpResponse = null;
    		try {
    			closeableHttpResponse = httpClient.execute(httpPost);
    
    			// 解析响应,返回结果
    			if (closeableHttpResponse.getStatusLine().getStatusCode() == 200) {
    				// 判断响应体是否不为空
    				if (closeableHttpResponse.getEntity() != null) {
    					String content = EntityUtils.toString(closeableHttpResponse.getEntity(), "UTF-8");
    					return content;
    				}
    
    			}
    		} catch (IOException e) {
    			e.printStackTrace();
    		} finally {
    			try {
    				if (closeableHttpResponse != null) {
    					// 关闭连接
    					closeableHttpResponse.close();
    				}
    				// 不能关闭,现在使用的是连接管理器
    				// httpClient.close();
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    
    		}
    		return null;
    
    	}
    
    
    }
    
  • 相关阅读:
    Linux下视频采集及其显示
    编译webcam_server
    mjpgstreamer
    linux下ffmpeg的安装
    armlinuxgcc的命令参数介绍
    ADS1.2如何生成BIN文件
    OpenJTAG下载bootloader 备忘
    Android 3.0 以下 使用Fragment 实例
    Dialog 学习备忘
    CentOS安装wsgi
  • 原文地址:https://www.cnblogs.com/lyd447113735/p/14923751.html
Copyright © 2020-2023  润新知