httpclient 实现的http工具类

HttpClient实现的工具类

就是簡單的用http 協議請求請求地址並返回數據,廢話少數直接上代碼

http請求返回的封裝類

    package com.nnk.upstream.util;

    import org.apache.http.cookie.Cookie;

    import java.util.HashMap;
    import java.util.List;

    /**
     * http请求返回结果封装
     */
    public class HttpResponse {
        //返回的數據
        private String content=null;
        //返回的cookies
        private HashMap<String, String> cookie=null;
        //重定向的地址
        private String location=null;
        //記錄時間
        private String date;
        //返回的狀態
        private int status;
        public String getContent() {
            return content;
        }
        public void setContent(String content) {
            this.content = content;
        }
        public String getLocation() {
            return location;
        }
        public void setLocation(String location) {
            this.location = location;
        }
        public HashMap<String, String> getCookie() {
            return cookie;
        }
        public void setCookie(HashMap<String, String> cookie) {
            this.cookie = cookie;
        }
        public String getDate() {
            return date;
        }
        public void setDate(String date) {
            this.date = date;
        }

        public int getStatus() {
            return status;
        }

        public void setStatus(int status) {
            this.status = status;
        }

        private List<Cookie> cookieList;

        public List<Cookie> getCookieList() {
            return cookieList;
        }

        public void setCookieList(List<Cookie> cookieList) {
            this.cookieList = cookieList;
        }
    }

httpUtils 工具類

    package com.nnk.upstream.util;

    import java.io.File;
    import java.io.FileOutputStream;
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map.Entry;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    import org.apache.http.Header;
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpHost;
    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.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.client.protocol.HttpClientContext;
    import org.apache.http.cookie.Cookie;
    import org.apache.http.impl.client.AbstractHttpClient;
    import org.apache.http.impl.client.BasicCookieStore;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.protocol.BasicHttpContext;
    import org.apache.http.protocol.HttpContext;
    import org.apache.http.util.EntityUtils;
    import org.apache.log4j.Logger;



    /**
     * Http连接工具类
     */
    public class HttpUtil {

        private static Logger log=Logger.getLogger(HttpUtil.class);
        public static RequestConfig getDefConf() {
            return RequestConfig.custom()  //设置连接超时
            .setSocketTimeout(60000)
            .setConnectTimeout(60000)
            .setConnectionRequestTimeout(50000)
            .build();
        }
        /**
         * 发送get请求并接收返回结果
         * @param url 请求的地址
         * @param charset 编码方式
         * @return response的bean实例
         */
        public static HttpResponse get(String url,HashMap<String, String> cookie,String charset){
            CloseableHttpClient httpClient=HttpClients.custom()
                    .setDefaultRequestConfig(getDefConf())
                    .build();;
            HttpResponse httpResponse=new HttpResponse();
            try{
                HttpGet httpGet=new HttpGet(url);
                StringBuilder reqCookieStr=new StringBuilder();
                Iterator<Entry<String, String>> iterator=cookie.entrySet().iterator();
                while (iterator.hasNext()){
                    Entry<String, String> entry=iterator.next();
                    reqCookieStr.append(entry.getKey()).append("=").append(entry.getValue()).append("; ");
                }
                httpGet.addHeader("Cookie",reqCookieStr.toString());
                httpGet.addHeader("User-Agent","Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36");
                httpGet.addHeader("Connection","Keep-Alive");
                httpGet.addHeader("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
                httpGet.addHeader("Accept-Encoding","gzip, deflate");
                httpGet.addHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
                //以下header要留着
                httpGet.addHeader("Referer","http://wap.10010.com/t/home.htm");
                CloseableHttpResponse response=httpClient.execute(httpGet);
                HttpEntity entity=response.getEntity();
                int statusCode = response.getStatusLine().getStatusCode();
                log.info("status:"+response.getStatusLine());
                httpResponse.setStatus(statusCode);
                if(statusCode==200||statusCode==302){
                    Header[] responseHeaders=response.getAllHeaders();
                    for(Header header:responseHeaders){
                        if(header.getName().equals("Set-Cookie")){
                            String[] jseeiond = header.getValue().split(";")[0].split("=");
                                cookie.put(jseeiond[0],jseeiond[1]);
                        }else if(header.getName().equals("Location")){
                            httpResponse.setLocation(header.getValue());
                        }
                    }
                    httpResponse.setContent(EntityUtils.toString(entity,charset));
                    httpResponse.setCookie(cookie);
                }else{
                    log.error("访问Url异常:"+url);
                }
                try{
                    response.close();
                }catch (Exception e) {
                    log.error(e);
                }
            }catch(Exception e){
                e.printStackTrace();
            }
            return httpResponse;
        }

        /**
         * 发送post请求并接收返回结果
         * @param url 请求的地址
         * @param cookie 请求的cookie
         * @param nvps post请求的参数
         * @param responseCharSet 返回内容的编码方式
         * @return 发送信息的处理结果
         */
        public static HttpResponse post(String url,HashMap<String, String> cookie,List<NameValuePair> nvps,String responseCharSet){
            CloseableHttpClient httpClient=HttpClients.custom()
                    .setDefaultRequestConfig(getDefConf())
                    .build();;
            HttpResponse httpResponse=new HttpResponse();
            try{
                HttpPost httpPost=new HttpPost(url);
                StringBuilder reqCookieStr=new StringBuilder();
                Iterator<Entry<String, String>> iterator=cookie.entrySet().iterator();
                while (iterator.hasNext()){
                    Entry<String, String> entry=iterator.next();
                    reqCookieStr.append(entry.getKey()).append("=").append(entry.getValue()).append("; ");
                }
                httpPost.addHeader("Cookie",reqCookieStr.toString());
                httpPost.addHeader("User-Agent","Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36");
                httpPost.addHeader("Connection","Keep-Alive");
                httpPost.addHeader("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
                httpPost.addHeader("Accept-Encoding","gzip, deflate");
                httpPost.addHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
                //以下header要留着
                httpPost.addHeader("Referer","http://wap.10010.com/t/home.htm");
                httpPost.setEntity(new UrlEncodedFormEntity(nvps,responseCharSet));
                log.info("post datas:" + EntityUtils.toString(httpPost.getEntity()));
                CloseableHttpResponse response=httpClient.execute(httpPost);

                try{
                    HttpEntity entity=response.getEntity();
                    Header[] responseHeaders=response.getAllHeaders();
                    int statusCode = response.getStatusLine().getStatusCode();
                    log.info("status:" + response.getStatusLine());
                    httpResponse.setStatus(statusCode);
                    for (Header header : responseHeaders) {
                        if (header.getName().equals("Set-Cookie")) {
                            Matcher matcher = Pattern.compile("(.+?)=(.*?);\\s{0,1}Domain").matcher(header.getValue());
                            while (matcher.find()) {
                                cookie.put(matcher.group(1), matcher.group(2));
                            }
                        } else if (header.getName().equals("Location")) {
                            httpResponse.setLocation(header.getValue());
                        }
                    }

                    httpResponse.setContent(EntityUtils.toString(entity, responseCharSet));
                    httpResponse.setCookie(cookie);
                }finally{
                    response.close();
                }
            }catch(Exception e){
                e.printStackTrace();
            }finally{
            }
            return httpResponse;
        }

        /**
         * 发送post请求并接收返回结果
         * @param url 请求的地址
         * @param cookie 请求的cookie
         * @param nvps post请求的参数
         * @return 发送信息的处理结果
         */
        public static HttpResponse downLoad(String url,HashMap<String, String> cookie,List<NameValuePair> nvps,File file){
            CloseableHttpClient httpClient=HttpClients.custom()
                    .setDefaultRequestConfig(getDefConf())
                    .build();;
            HttpResponse httpResponse=new HttpResponse();
            try{
                HttpPost httpPost=new HttpPost(url);
                StringBuilder reqCookieStr=new StringBuilder();
                Iterator<Entry<String, String>> iterator=cookie.entrySet().iterator();
                while (iterator.hasNext()){
                    Entry<String, String> entry=iterator.next();
                    reqCookieStr.append(entry.getKey()).append("=").append(entry.getValue()).append("; ");
                }
                httpPost.addHeader("Cookie",reqCookieStr.toString());
                httpPost.addHeader("User-Agent","Mozilla/5.0 (Windows NT 6.3; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0");
                httpPost.setEntity(new UrlEncodedFormEntity(nvps,"GB2312"));
                CloseableHttpResponse response=httpClient.execute(httpPost);
                try{
                    HttpEntity entity=response.getEntity();
                    response.getStatusLine().getStatusCode();
                    Header[] responseHeaders=response.getAllHeaders();
                    for(Header header:responseHeaders){
                        if(header.getName().equals("Set-Cookie")){
                            Matcher matcher=Pattern.compile("(.+?)=(.*?);\\s{0,1}Domain").matcher(header.getValue());
                            while(matcher.find()){
                                cookie.put(matcher.group(1),matcher.group(2));
                            }
                        }else if(header.getName().equals("Location")){
                            httpResponse.setLocation(header.getValue());
                        }
                    }
                    FileOutputStream fos=new FileOutputStream(file);
                    fos.write(EntityUtils.toByteArray(entity));
                    fos.close();
                    httpResponse.setCookie(cookie);
                }finally{
                    response.close();
                }
            }catch(Exception e){
                e.printStackTrace();
            }finally{
            }
            return httpResponse;
        }

        public static String htmlEscape(String content){
            content=content.replace("&amp;","&").replace("&#37;","%").replace("&lt;","<").replace("&gt;",">");
            return content;
        }


        public static HttpResponse sendGet(String url,Cookie[] cookies,String charset) {
            HttpResponse httpResponse = new HttpResponse();
            //1.获得一个httpclient对象
            CloseableHttpClient httpclient = HttpClients.custom()
                    .setDefaultRequestConfig(getDefConf())
                    .build();

            CloseableHttpResponse response = null;
            try {
                // 设置cookies
                BasicCookieStore cookieStore = new BasicCookieStore();
                cookieStore.addCookies(cookies);

                HttpContext httpContext = new BasicHttpContext();
                httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);

                //2.生成一个get请求
                HttpGet httpget = new HttpGet(url);
                httpget.addHeader("User-Agent","Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36");
                httpget.addHeader("Connection","Keep-Alive");
                httpget.addHeader("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
                httpget.addHeader("Accept-Encoding","gzip, deflate");
                httpget.addHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
                //以下header要留着
                httpget.addHeader("Referer","http://wap.10010.com/t/home.htm");
                BasicCookieStore cookieStore2;
                //3.执行get请求并返回结果
                response = httpclient.execute(httpget, httpContext);
                cookieStore2 = (BasicCookieStore)httpContext.getAttribute(HttpClientContext.COOKIE_STORE);
                //返回内容设置
                httpResponse.setContent(EntityUtils.toString(response.getEntity(),charset));
                int stauts = response.getStatusLine().getStatusCode();
                //返回状态设置
                httpResponse.setStatus(stauts);
                //设置返回的Cookie
                List<Cookie> cookies1 = cookieStore2.getCookies();
                cookies1.addAll(Arrays.asList(cookies));
                httpResponse.setCookieList(cookies1);
                Header[] responseHeaders=response.getAllHeaders();
                for(Header header:responseHeaders){
                    if(header.getName().equals("Location")){
                        httpResponse.setLocation(header.getValue());
                    }
                }
            } catch (Exception e1) {
                e1.printStackTrace();
                log.info("请求异常.....");
            }finally {
                try {
                    if(null != response) {
                        response.close();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return httpResponse;
        }


        public static HttpResponse sendPost(String url,Cookie[] cookies,List<NameValuePair> nvps ,String charset) {
            HttpResponse httpResponse = new HttpResponse();
            //1.获得一个httpclient对象
            CloseableHttpClient httpclient = HttpClients.custom()
                    .setDefaultRequestConfig(getDefConf())
                    .build();



            CloseableHttpResponse response = null;
            BasicCookieStore cookieStore2;
            try {
                // 设置cookies
                BasicCookieStore cookieStore = new BasicCookieStore();
                cookieStore.addCookies(cookies);
                HttpContext httpContext = new BasicHttpContext();
                httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);

                //2.生成一个get请求
                HttpPost httppost = new HttpPost(url);
                httppost.addHeader("User-Agent","Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36");
                httppost.addHeader("Connection","Keep-Alive");
                httppost.addHeader("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
                httppost.addHeader("Accept-Encoding","gzip, deflate");
                httppost.addHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
                //以下header要留着
                httppost.addHeader("Referer","http://wap.10010.com/t/home.htm");
                HttpEntity entity = new UrlEncodedFormEntity(nvps,charset);
                log.info("post data:"+EntityUtils.toString(entity));
                httppost.setEntity(entity);
                //3.执行get请求并返回结果
                response = httpclient.execute(httppost, httpContext);
                cookieStore2 = (BasicCookieStore)httpContext.getAttribute(HttpClientContext.COOKIE_STORE);
                //返回内容设置
                httpResponse.setContent(EntityUtils.toString(response.getEntity(),charset));
                int stauts = response.getStatusLine().getStatusCode();
                //返回状态设置
                httpResponse.setStatus(stauts);
                //设置返回的Cookie
                List<Cookie> cookies1 = cookieStore2.getCookies();
                cookies1.addAll(Arrays.asList(cookies));
                httpResponse.setCookieList(cookies1);
                Header[] responseHeaders=response.getAllHeaders();
                for(Header header:responseHeaders){
                    if(header.getName().equals("Location")){
                        httpResponse.setLocation(header.getValue());
                    }
                }
            } catch (Exception e1) {
                e1.printStackTrace();
                log.info("请求异常.....");
            }finally {
                try {
                    if(null != response) {
                        response.close();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return httpResponse;
        }
    }

封装重定向的HttpProxyUtils

    package com.nnk.upstream.util;
    import com.nnk.interfacetemplate.common.StringUtil;
    import org.apache.commons.lang3.ArrayUtils;
    import org.apache.http.NameValuePair;
    import org.apache.http.impl.cookie.BasicClientCookie;
    import org.apache.log4j.Logger;

    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.*;

    /**
     * Created with IntelliJ IDEA.
     * User: xxydl
     * Date: 2016/11/11
     * Time: 19:12
     * email: [email protected]
     * To change this template use File | Settings | File Templates.
     */
    public class HttpProxyUtil {
        public static final Logger log = Logger.getLogger(HttpProxyUtil.class);
        public static HttpResponse get(String url,List<org.apache.http.cookie.Cookie> cookielist,String charset){
            boolean ret = true;
            HttpResponse response = null;
            do{
                if(StringUtil.isEmpty(url)){
                    log.warn("url is Empty");
                    break;
                }else{
                    log.info("get url:" + url);
                    org.apache.http.cookie.Cookie[] arr = (org.apache.http.cookie.Cookie[])cookielist.toArray(new org.apache.http.cookie.Cookie[cookielist.size()]);
                    response = HttpUtil.sendGet(url, arr, charset);
                }
                if(response!=null && 200==response.getStatus()){
                    return response;
                }else if(response!=null && 302==response.getStatus()){
                    url = response.getLocation();
                    cookielist = response.getCookieList();
                    log.info("重定向到Url:" + url);
                    log.info("cookies:" + response.getCookieList());
                }else {
                    return response;
                }
            }while (true);
            return response;
        }

        public static HttpResponse post(String url,List<org.apache.http.cookie.Cookie> cookielist,List<NameValuePair> nvps,String charset){
            boolean isPost = true;
            HttpResponse response = null;
            do{
                org.apache.http.cookie.Cookie[] arr = (org.apache.http.cookie.Cookie[])cookielist.toArray(new org.apache.http.cookie.Cookie[cookielist.size()]);
                if(StringUtil.isEmpty(url)){
                    log.warn("url is Empty");
                    break;
                }else if(isPost){
                    log.info("post url:" + url);

                    response = HttpUtil.sendPost(url, arr, nvps, charset);
                }else {
                    response = HttpUtil.sendGet(url, arr, charset);
                }
                if(response!=null && 200==response.getStatus()){
                    return response;
                }else if(response!=null && 302==response.getStatus()){
                    url = response.getLocation();
                    log.info("重定向到Url:" + url);
                    log.info("cookies:" + response.getCookieList());
                    cookielist = response.getCookieList();
                    isPost = false;
                }else {
                    return response;
                }
            }while (true);
            return response;
        }

以上有些方法可能没有自己简单实现就可以了。

猜你喜欢

转载自blog.csdn.net/a153870727/article/details/53197801