基于HttpClient4.3实现的Http后台请求

首先封装HTTP请求工具类


public class HttpHelper

{
    private PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
    
    private CloseableHttpClient httpClient;
    
    private HttpHost httpHost;
    
    private String path;
    
    private int timeout;
    
    private RequestConfig requestConfig;
    
    public HttpHelper(String ip, int port, int timeout)
    {
        //超时时长设置
        this.timeout = timeout * 1000;
        
        //设置连接池最大并发连接
        connManager.setMaxTotal(1000);
        
        //设置单个路由最大连接,覆盖默认值2
        connManager.setDefaultMaxPerRoute(10);
        
        //初始化httpClient
        httpClient = HttpClients.custom().setConnectionManager(connManager).build();
        
        //请求目标服务器设置
        httpHost = new HttpHost(ip, port);
        
        //设置全局请求参数
        RequestConfig defaultRequestConfig =
            RequestConfig.custom()
                .setCookieSpec(CookieSpecs.BEST_MATCH)
                .setExpectContinueEnabled(true)
                .setStaleConnectionCheckEnabled(true)
                .setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST))
                .setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC))
                .build();
        
        requestConfig = RequestConfig.copy(defaultRequestConfig).setConnectTimeout(this.timeout).build();
    }
    
    /**
     * @Title: getResponse
     * @Description: 请求HTTP服务器上的内容并解析返回结果
     * @param @return
     * @param @throws Exception
     * @return String
     * @throws
     */
    public String getResponse()
        throws Exception
    {
        try
        {
            //设置协议头相关信息
            HttpGet httpGet = new HttpGet(path);
            httpGet.setHeader("User-Agent", "mdn");
            httpGet.setHeader("Connection", "Keep-Alive");
            httpGet.setConfig(requestConfig);
            
            //请求返回值句柄
            CloseableHttpResponse resp = httpClient.execute(httpHost, httpGet);
            
            //创建响应处理器处理服务器响应内容  
            //ResponseHandler<String> responseHandler = new BasicResponseHandler();  
            //执行请求并获取结果  
            //String responseBody = httpClient.execute(httpGet, responseHandler);  
            try
            {
                //请求返回结果为成功
                //200         请求成功
                //201     请求完成,结果是创建了新资源
                //202      请求被接受,但处理还没完成
                //204     服务器已经完成了请求,但是没有返回新的信息
                //300     存在多个可用的被请求资源
                //301     请求道的资源都会分配一个永久的url
                //302     请求道的资源放在一个不同的url中临时保存
                //304     请求的资源未更新
                //400      非法请求
                //401     未授权
                //403      禁止
                //404      找不到页面
                if (HttpStatus.SC_OK == resp.getStatusLine().getStatusCode())
                {
                    // 获取返回对象
                    HttpEntity entity = resp.getEntity();
                    if (entity != null)
                    {
                        //获取返回对象的内容流
                        InputStream instream = entity.getContent();
                        try
                        {
                            //读取内容流进行处理
                            int l;
                            byte[] tmp = new byte[2048];
                            StringBuffer bufferResp = new StringBuffer();
                            while ((l = instream.read(tmp)) != -1)
                            {
                                bufferResp.append(new String(tmp, 0, l, "UTF-8"));
                            }
                            EntityUtils.consume(resp.getEntity());
                            return bufferResp.toString();
                        }
                        catch (IOException ex)
                        {
                            throw ex;
                        }
                        finally
                        {
                            //关闭流
                            if(null != instream)
                            {
                                instream.close();
                            }
                        }
                    }
                    else
                    {
                        return null;
                    }
                }
                else
                {
                    return null;
                }
            }
            finally
            {
                if (null != resp)
                {
                    resp.close();
                }
            }
        }
        finally
        {
            if (null != httpClient)
            {
                httpClient.close();
            }
        }
    }
    
    /**
     * @Title: setPath
     * @Description: 设置请求路径
     * @param @param path
     * @return void
     * @throws
     */
    public void setPath(String path)
    {
        if (path == null || path.equals(""))
        {
            this.path = "";
        }
        else if (path.charAt(0) == '/')
        {
            this.path = path;
        }
        else
        {
            this.path = "/" + path;
        }
    }

}




测试类:

public class HttpTester
{
    public static void main(String[] args)
    {
        HttpHelper httpClient = new HttpHelper("10.137.13.193", 8080, 10);
        httpClient.setPath("/httpserver?filename=CMS.mpd");
            String response = "";
            try
            {
                response = httpClient.getResponse();
                System.out.println(response);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            System.out.println(response);
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值