码迷,mamicode.com
首页 > Web开发 > 详细

Apache HttpClient组件封装工具类

时间:2015-08-02 15:09:04      阅读:346      评论:0      收藏:0      [点我收藏+]

标签:

package com.mengyao.spider.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
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.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.http.NameValuePair;

/**
 * 依赖于Apache httpcomponents项目下的HttpClient组件中的httpclient-4.4.jar、httpcore-4.4.jar、commons-logging-1.2.jar
 * @author mengyao
 *
 */
public class HttpUtil {

    /**
     * 创建httpClient实例
     * @return
     */
    public CloseableHttpClient getHttpClient() {
        HttpClientBuilder builder = HttpClients.custom();
        CloseableHttpClient client = builder.build();
        return client;
    }
    
    /**
     * 构造Post请求
     * @param url
     * @param params
     * @param encoding
     * @return
     * @throws Exception
     */
    public HttpPost getHttpPost(String url, Map<String, String> params, String encoding) throws Exception{
        HttpPost post = new HttpPost(url);
        List<NameValuePair> parammeters = new ArrayList<NameValuePair>();
        Iterator<Entry<String, String>> iterator = params.entrySet().iterator();
        while(iterator.hasNext()){
            Entry<String, String> next = iterator.next();
            parammeters.add(new BasicNameValuePair(next.getKey(), next.getValue()));
        }
        UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(parammeters, encoding);
        post.setEntity(urlEncodedFormEntity);
        
        return post;
    }
    
    /**
     * 构造Get请求
     * @param url
     * @return
     */
    public HttpGet getHttpGet(String url){
        HttpGet get = new HttpGet(url);
        return get;
    }
    
    /**
     * 为Post或Get请求设置Http请求头参数
     * @param postAndGet
     * @param headers
     * @return
     */
    public HttpRequestBase setHeader(HttpRequestBase postAndGet, Map<String, String> headers){
        Iterator<Entry<String, String>> iterator = headers.entrySet().iterator();
        while(iterator.hasNext()){
            Entry<String, String> next = iterator.next();
            postAndGet.addHeader(next.getKey(), next.getValue());
        }
        return postAndGet;
    }
    
    /**
     * 获取Http响应中的响应头参数
     * @param response
     * @return
     */
    public Map<String, String> getHeader(CloseableHttpResponse response){
        Map<String, String> headers = new HashMap<String, String>();
        Header[] allHeaders = response.getAllHeaders();
        for (Header header : allHeaders) {
            headers.put(header.getName(), header.getValue());
        }
        return headers;
    }
    
    /**
     * 获取Http响应中的原生数据
     * @param entity
     * @param consume
     * @return
     * @throws Exception
     */
    public String getRawContent(HttpEntity entity, boolean consume) throws Exception{
        String rawCtx = EntityUtils.toString(entity);
        if (consume) {
            EntityUtils.consume(entity);
        }
        return rawCtx;
    }
    
    /**
     * 释放Http连接
     * @param client
     * @throws Exception
     */
    public void clean(CloseableHttpClient client) throws Exception{
        client.close();
    }
    
    public static void main(String[] args) throws Exception {
        HttpUtil httpUtil = new HttpUtil();
        /************************************* 创建HttpGet请求Begin ***********************************/
        //获取Http实例
        CloseableHttpClient httpClient = httpUtil.getHttpClient();
        //创建HttpGet请求
        HttpGet httpGet = httpUtil.getHttpGet("http://www.jd.com");
        //设置HttpGet请求头参数
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0");
        httpUtil.setHeader(httpGet, headers);
        //提交HttpGet请求,同时获取Http响应
        CloseableHttpResponse response = httpClient.execute(httpGet);
        //获取Http的响应头参数
        Map<String, String> header = httpUtil.getHeader(response);
        //获取Http响应中的原生数据内容,同时关闭Http底层连接
        String rawContent = httpUtil.getRawContent(response.getEntity(), true);
        //释放本次Http请求实例
        httpUtil.clean(httpClient);
        /************************************* 创建HttpGet请求End ***********************************/
        
        //HttpPost请求与上述代码同理,不做演示
    }
}

Apache HttpClient组件封装工具类

标签:

原文地址:http://www.cnblogs.com/mengyao/p/4695744.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!