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("&","&").replace("%","%").replace("<","<").replace(">",">");
            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: xxydliuy@163.com
     * 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;
        }以上有些方法可能没有自己简单实现就可以了。
httpclient 实现的http工具类的更多相关文章
- HttpClient 4.5.x 工具类设计与实现
		最近,业务需要在java服务端发起http请求,需要实现"GET","POST","PUT"等基本方法.于是想以 "HttpCli ... 
- 使用HttpClient访问url的工具类
		maven依赖jar包配置: <dependency> <groupId>org.apache.httpcomponents</groupId> <artif ... 
- Httpclient 工具类(get,put)
		package com.googosoft.until; import java.io.IOException; import org.apache.http.HttpEntity; import o ... 
- HttpUtil工具类,发送Get/Post请求,支持Http和Https协议
		HttpUtil工具类,发送Get/Post请求,支持Http和Https协议 使用用Httpclient封装的HttpUtil工具类,发送Get/Post请求 1. maven引入httpclien ... 
- 使用单例模式实现自己的HttpClient工具类
		引子 在Android开发中我们经常会用到网络连接功能与服务器进行数据的交互,为此Android的SDK提供了Apache的HttpClient 来方便我们使用各种Http服务.你可以把HttpCli ... 
- 基于HttpClient 4.3的可訪问自签名HTTPS网站的新版工具类
		本文出处:http://blog.csdn.net/chaijunkun/article/details/40145685,转载请注明.因为本人不定期会整理相关博文,会对相应内容作出完好.因此强烈建议 ... 
- Android开发实现HttpClient工具类
		在Android开发中我们经常会用到网络连接功能与服务器进行数据的交互,为此Android的SDK提供了Apache的HttpClient来方便我们使用各种Http服务.你可以把HttpClient想 ... 
- http和https工具类 (要注意httpclient版本号和log4j的版本号)
		1 工具类 package dd.com; import java.io.IOException; import java.security.cert.CertificateException; im ... 
- 170314、工具:apache httpClient多线程并发情况下安全实用及工具类分享
		简单用法介绍:介绍来源网络 建立连接:在HttpClient中使用多线程的一个主要原因是可以一次执行多个方法.在执行期间,每一个方法都使用一个HttpConnection实例.由于在同一时间多个连接只 ... 
随机推荐
- Web jsp开发学习——数据库的另一种连接方式(配置静态数据库连接池)
			1.导包 2.找到sever里的sever.xml,配置静态数据库连接池 <Context docBase="bookstore" path="/booksto ... 
- python中复制文件
			1.复制单个文件 1.把home下的test.yml复制到root目录下 In [43]: import shutil In [42]: shutil.copy('/home/test.yml','/ ... 
- 例子 使用sqlite3 数据库建立数据方式
			#!/usr/bin/env python#coding:utf-8import sqlite3#建立一个数据库cx = sqlite3.connect("E:/test.db") ... 
- Win10黑色白色主题切换
			打开设置面板 选择个性化 -> 颜色 -> 选择默认应用模式 然后选择亮和暗就好了. (这还是看404说的我才知道.) 
- pandas 分组统计
			# coding:utf-8 import pandas as pd import numpy as np # path = r'C:\Users\wuzaipei\Desktop\桂林三金项目签到情 ... 
- 【并行计算-CUDA开发】OpenCL、OpenGL和DirectX三者的区别
			什么是OpenCL? OpenCL全称Open Computing Language,是第一个面向异构系统通用目的并行编程的开放式.免费标准,也是一个统一的编程环境,便于软件开发人员为高性能计算服务器 ... 
- [转帖]流言终结者 —— “SQL Server 是Sybase的产品而不是微软的”
			流言终结者 —— “SQL Server 是Sybase的产品而不是微软的” https://www.cnblogs.com/xxxtech/archive/2011/12/30/2307859.ht ... 
- Go语言中的数组(九)
			我刚接触go语言的数组时,有点不习惯,因为相对于JavaScript这样的动态语言里的数组,go语言的数组写起来有点不爽. 定义数组 go语言定义数组的格式如下: ]int var 数组名 [数组长度 ... 
- # IDEA相关知识
			目录 IDEA相关知识 安装目录下: 配置目录下: 工程目录下: 名词解释 IDEA相关知识 安装目录下: bin:启动文件,配置信息,IDEA的一些属性信息 jre64:IDEA自带的运行环境 li ... 
- 数据库索引 B+树
			问题1.数据库为什么要设计索引?索引类似书本目录,用于提升数据库查找速度.问题2.哈希(hash)比树(tree)更快,索引结构为什么要设计成树型?加快查找速度的数据结构,常见的有两类:(1)哈希,例 ... 
