HttpQuery httpClient with cookie sessionID

本文将介绍如何使用Apache HttpClient库进行HTTP GET和POST请求,并详细解释如何配置请求参数、处理响应、解析cookie和headers。同时,还提供了一个简单的HTTP工具类,用于简化HTTP请求操作。

httpClient-4.3.3  实用工具,提供个了cookie功能


maven:

<dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.3.3</version>
</dependency>



HttpQuery.java

import org.apache.http.Consts;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
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.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import java.net.URI;
import java.net.URL;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HttpQuery {
    private static Logger log = Logger.getLogger(HttpQuery.class);

    private CloseableHttpClient httpClient = null;
    private HttpClientBuilder builder = null;
    private HttpGet httpGet = null;
    private HttpPost httpPost = null;
    private Header[] headers = null;
    private String cookies = null;
    private String referer;
    private Map<String, String> cookieMap = new HashMap<String, String>();
    private Boolean autoClose = true;
    private String lastError;

    public HttpQuery(){
        this(true);
    }

    public HttpQuery(boolean autoClose){
        this.autoClose = autoClose;
    }

    public void setAutoClose(Boolean autoClose) {
        this.autoClose = autoClose;
    }

    /**
     * 在HttpClient 4.0+的版本,正确的关闭连接如下:
     * EntityUtils.consumeQuietly(response.getEntity());
     * 在HttpClient 3.0+版本中,正确的关闭如下:
     * method.releaseConnection();
     */
    private void init() {
        if (this.httpClient == null) {
            builder = HttpClientBuilder.create();
            RequestConfig.Builder reqBuild = RequestConfig.copy(RequestConfig.DEFAULT);
            reqBuild.setConnectTimeout(30 * 1000);
            reqBuild.setSocketTimeout(30 * 1000);
            reqBuild.setRedirectsEnabled(true);
            RequestConfig reqConfig = reqBuild.build();
//            builder.setRetryHandler(new DefaultHttpRequestRetryHandler());//默认失败后重发3次
            builder.setDefaultRequestConfig(reqConfig);
            builder.setUserAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36");
            this.httpClient = builder.build();
        }
    }

    public String httpGet(String url, Map<String, Object> parameters) throws Exception {
        this.init();

        URL reqURL = new URL(url);
        URI urlTemp = reqURL.toURI();
        URIBuilder uriBuilder = new URIBuilder().setScheme(urlTemp.getScheme()).setHost(urlTemp.getHost()).setPort(urlTemp.getPort()).setPath(urlTemp.getPath());
        String query =  reqURL.getQuery();
        uriBuilder.setParameters(URLEncodedUtils.parse(query, Consts.UTF_8));

        if (parameters != null && parameters.size() > 0) {
            Iterator it = parameters.keySet().iterator();
            while (it.hasNext()) {
                String key = (String) it.next();
                if (key == null)
                    continue;
                if (parameters.get(key) != null) {
                    uriBuilder.setParameter(key, parameters.get(key).toString());
                } else {
                    uriBuilder.setParameter(key, "");
                }
            }
        }

        URI uri = uriBuilder.build();
        this.httpGet = new HttpGet(uri);
        RequestConfig.Builder reqBuild = RequestConfig.copy(RequestConfig.DEFAULT);
        RequestConfig requestConfig = reqBuild.build();
        httpGet.setConfig(requestConfig);

        if (this.headers == null)
            this.headers = httpGet.getAllHeaders();
        httpGet.addHeader("Referer", referer);
        httpGet.setHeader("Cookie", this.getCookies());
        HttpResponse response = httpClient.execute(httpGet);
        this.referer = url;
        this.parseCookie(response);
        this.parseHeaders(response);

        String html = null;
        if (response != null) {
            html = EntityUtils.toString(response.getEntity());
            EntityUtils.consumeQuietly(response.getEntity());//在HttpClient 4.0+的版本,正确的关闭连接
        }
        if (this.autoClose)
            this.close();
        return html;
    }

    public int getCode(){
        return this.respCode;
    }

    private Header contentType;
    private void setContentType(Header contentType){
        if(contentType!=null) {
            this.contentType = contentType;
            String content = this.contentType.getValue();
            Pattern pattern = Pattern.compile("charset\\=(.*)");
            Matcher m = pattern.matcher(content);
            if(m.find()){
                this.contentEncoding = m.group(1);
            }
        }
    }
    public String getContentType(){
        if(this.contentType!=null)
            return this.contentType.getValue();
        return "";
    }

    public void close() {
        if (this.httpClient != null) {
            try {
                this.httpClient.close();
            } catch (Exception e) {
                e.printStackTrace();
                log.error("httpClient.close error", e);
            }
            this.httpClient = null;
            this.builder = null;
        }
    }

    private void parseCookie(HttpResponse response) {
        Header[] cookieHeaders = response.getHeaders("Set-Cookie");
        if (this.cookies == null)
            this.cookies = "";
        for (Header s : cookieHeaders) {
            String str = s.toString().substring("Set-Cookie:".length());
            String[] temps = str.split(";");
            if (temps != null) {
                for (String temp : temps) {
                    if (temp != null) {
                        int index = temp.indexOf("=");
                        if (index > 0 && temp.length() > (index + 1)) {
                            this.cookieMap.put(temp.substring(0, index), temp.substring(index));
                        }
                    }
                }
            }
        }
    }

    public Map getCookie(){
        return this.cookieMap;
    }

    private String getCookies() {
        StringBuffer sb = new StringBuffer();
        String[] keys = this.cookieMap.keySet().toArray(new String[0]);
        for (String key : keys) {
            sb.append(key);
            sb.append(this.cookieMap.get(key));
            sb.append(";");
        }
        return sb.toString();
    }

    public Header[] getHeaders(){
        return this.headers;
    }

    public int getRespCode(){
        return this.respCode;
    }

    private int respCode = 200;

    private Object parseHeaders(HttpResponse response) throws Exception {
        this.headers = response.getAllHeaders();
        this.respCode = response.getStatusLine().getStatusCode();
        this.setContentType(response.getEntity().getContentType());
        return null;
    }

    public String httpPost(String url) throws Exception {
        return this.httpPost(url, new HashMap<String, Object>());
    }

    public String httpPost(String url, List<NameValuePair> params) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        for (NameValuePair nameValuePair : params) {
            map.put(nameValuePair.getName(), nameValuePair.getValue());
        }
        return this.httpPost(url, map);
    }

    public String httpPost(String url, Map<String, Object> parameters) throws Exception {
        this.init();

        URL reqURL = new URL(url);
        URI urlTemp = reqURL.toURI();
        URIBuilder uriBuilder = new URIBuilder().setScheme(urlTemp.getScheme()).setHost(urlTemp.getHost()).setPort(urlTemp.getPort()).setPath(urlTemp.getPath());
        String query =  reqURL.getQuery();
        uriBuilder.setParameters(URLEncodedUtils.parse(query, Consts.UTF_8));
        URI uri = uriBuilder.build();

        this.httpPost = new HttpPost(uri);
        List<NameValuePair> list = null;
        if (parameters != null && parameters.size() > 0) {
            list = new ArrayList<NameValuePair>();
            Iterator it = parameters.keySet().iterator();
            while (it.hasNext()) {
                String key = (String) it.next();
                if (key == null)
                    continue;
                if (parameters.get(key) != null)
                    list.add(new BasicNameValuePair(key, parameters.get(key).toString()));
                else
                    list.add(new BasicNameValuePair(key, ""));
            }
        }

        if (list != null && list.size() > 0)
            httpPost.setEntity(new UrlEncodedFormEntity(list, HTTP.UTF_8));

        if (this.headers == null)
            this.headers = httpPost.getAllHeaders();
        httpPost.addHeader("Referer", referer);
        httpPost.setHeader("Cookie", this.getCookies());
        HttpResponse response = httpClient.execute(httpPost);
        this.referer = url;
        this.parseCookie(response);
        this.parseHeaders(response);

        String html = null;
        if (response != null) {
            html = EntityUtils.toString(response.getEntity());
            EntityUtils.consumeQuietly(response.getEntity());//在HttpClient 4.0+的版本,正确的关闭连接
        }
        if (this.autoClose)
            this.close();
        return html;
    }

    private String contentEncoding = "UTF-8";
    public String getContentEncoding(){
        return this.contentEncoding;
    }

    public void sleep(long seconds) {
        for (int i = 0; i < seconds; i++) {
            try {
                Thread.sleep(1000);
                System.out.print(".");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("");
    }

}



HttpUtil


import org.apache.http.Header;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


public class HttpUtil {

    public static Object getFirstValueFromRequest(String key, HttpServletRequest request){
        Object value = null;
        //1. parameter
        value = request.getAttribute(key);
        if(value==null)
            value = request.getParameter(key);
        if(value==null)
            value = request.getHeader(key);
        if(value == null)
            value = getFirstCookie(key, request.getCookies());
        return value;
    }

    /**
     * 从request中读取参数
     * @param request
     * @param key
     * @return
     */
    public static String getStringFromRequest(HttpServletRequest request, String key){
        String value = (String)request.getAttribute(key);
        if(value==null)
            value = request.getParameter(key);
        if(value==null)
            value = request.getHeader(key);
        if(value==null)
            value = HttpUtil.getFirstCookie(key, request.getCookies());
        return value;
    }

    /**
     * 从cookie中读取第一个符合的参数
     * @param key
     * @param cookies
     * @return
     */
    public static String getFirstCookie(String key,Cookie[] cookies){
        if(cookies!=null && key!=null){
            for(Cookie cookie : cookies){
                if(key.equals(cookie.getName()))
                    return cookie.getValue();
            }
        }
        return null;
    }

    public static Map<String, Cookie> getCookieMap(HttpServletRequest request){
        Map<String, Cookie> map = new HashMap<String, Cookie>();
        Cookie[] cookies = request.getCookies();
        if(cookies!=null){
            for(Cookie cookie : cookies){
                map.put( cookie.getName()+cookie.getPath(), (Cookie)cookie.clone());
            }
        }
        return map;
    }

    public static String httpGet(String url,  Map<String,Object> parameters)throws Exception{
        HttpQuery httpQuery = new HttpQuery();
        String html = httpQuery.httpGet(url, parameters);
        httpQuery.close();
        return html;
    }
    public static Header[] httpGetHeaders(String url,  Map<String,Object> parameters)throws Exception{
        HttpQuery httpQuery = new HttpQuery();
        httpQuery.setAutoClose(false);
        httpQuery.httpGet(url, parameters);
        Header[] headers = httpQuery.getHeaders();
        return  headers;
    }

    public static String httpPost(String url,  Map<String,Object> parameters)throws Exception{
        HttpQuery httpQuery = new HttpQuery();
        String html = httpQuery.httpPost(url, parameters);
        return  html;
    }

    public static String newAccessToken(){
        String accessToken = UUID.randomUUID().toString();
        return accessToken;
    }

    public static String getCookie(HttpServletRequest request, String cookieName){
        Cookie[] cookies = request.getCookies();
        for(Cookie cookie : cookies){
            if(cookie.getName().trim().equals(cookieName.trim()))
                return cookie.getValue();
        }
        return null;
    }

    public static String getRemortIP(HttpServletRequest request) {
        // 取代理ip地址
        String ip = request.getHeader("x-forwarded-for");

        // 取nginx代理设置的ip地址
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("x-real-ip");

        }
        // 从网上取的
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        // 取JAVA获得的ip地址
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();

        }
        // 去除unkonwn
        if (ip.startsWith("unknown")) {
            ip = ip.substring(ip.indexOf("unknown") + "unknown".length());
        }
        // 去除多多余的信息
        ip = ip.trim();
        if (ip.startsWith(",")) {
            ip = ip.substring(1);
        }
        if (ip.indexOf(",") > 0) {
            ip = ip.substring(0, ip.indexOf(","));
        }
        return ip;
    }
}



Good luck!~


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值