• 简单HttpClientUtils工具类


    package com.zy.utils;
    
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpStatus;
    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.client.methods.HttpRequestBase;
    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.message.BasicNameValuePair;
    import org.apache.http.util.EntityUtils;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    
    public class HttpClientUtils {
        private static PoolingHttpClientConnectionManager connectionManager;
    
        static {
            //定义一个连接池的管理类对象
            connectionManager = new PoolingHttpClientConnectionManager();
            //定义连接池属性
            //定义连接池最大的连接数
            connectionManager.setMaxTotal(200);
            //定义主机的最大的并发数
            connectionManager.setDefaultMaxPerRoute(20);
        }
    
        //获取closeHttpClient
        private static CloseableHttpClient getCloseableHttpClient() {
    
            CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(connectionManager).build();
    
            return httpClient;
        }
    
    
        //执行请求返回HTML页面
        private static String execute(HttpRequestBase httpRequestBase) throws IOException {
    
            httpRequestBase.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36");
            /**
             * setConnectionRequestTimeout:设置获取请求的最长时间
             *
             * setConnectTimeout: 设置创建连接的最长时间
             *
             * setSocketTimeout: 设置传输超时的最长时间
             */
    
            RequestConfig config = RequestConfig.custom().setConnectionRequestTimeout(5000).setConnectTimeout(5000)
                    .setSocketTimeout(10 * 1000).build();
    
            httpRequestBase.setConfig(config);
    
    
            CloseableHttpClient httpClient = getCloseableHttpClient();
    
            CloseableHttpResponse response = httpClient.execute(httpRequestBase);
    
            // 请求发送成功,并得到响应
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                String html = EntityUtils.toString(response.getEntity(), "UTF-8");
    
                return html;
            } else {
                return "请求提交失败:" + httpRequestBase.getURI();
            }
        }
    
        //get请求执行
        public static String doGet(String url) throws IOException {
            HttpGet httpGet = new HttpGet(url);
    
            String html = execute(httpGet);
    
            return html;
    
        }
    
        //post请求执行
        public static String doPost(String url, Map<String, String> param) throws Exception {
            HttpPost httpPost = new HttpPost(url);
    
            List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
    
            for (String key : param.keySet()) {
    
                list.add(new BasicNameValuePair(key, param.get(key)));
            }
            HttpEntity entity = new UrlEncodedFormEntity(list);
            httpPost.setEntity(entity);
    
            return execute(httpPost);
        }
    }
  • 相关阅读:
    SQLServer提取日期中的年月日及其他格式
    大白话解说,半分钟就懂 --- 分布式与集群是什么 ? 区别是什么?
    VS2015 Git 源码管理工具简单入门
    Web.Config配置文件中customErrors元素的使用方法
    C#发起Http请求,调用接口
    如何停止和禁用Linux系统中的不需要的服务
    QtCreator调试传入运行参数
    gSOAP 在windows下的安装与使用(mingw32)
    MinGW 使用 mintty 终端替代默认终端以解决界面上复制与粘贴的问题
    在windows下执行./configure,make,makeinstall源码安装程序spice-gtk
  • 原文地址:https://www.cnblogs.com/blazeZzz/p/9411481.html
Copyright © 2020-2023  润新知