httpclient代码封装

import java.io.IOException;
import java.util.List;
import javax.net.ssl.SSLHandshakeException;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpRequest;
import org.apache.http.HttpVersion;
import org.apache.http.NoHttpResponseException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpRequestRetryHandler;
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.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.DefaultRedirectStrategy;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.apache.http.HttpResponse;

public class CustomerHttpClient {
    // 编码方式
    private static final String CHARSET = HTTP.UTF_8;
    // 唯一只会创建这一个
    private DefaultHttpClient customerHttpClient;
    private CookieStore cookies;
    private Header[] header;

    public CookieStore getCookies() {
        this.cookies = ((DefaultHttpClient) getHttpClient()).getCookieStore();
        return this.cookies;
    }

    public Header[] getHeader() {
        return header;
    }

    private void setCookie(BasicClientCookie cookie) {
        this.cookies = this.getCookies();
        this.cookies.addCookie(cookie);
        this.getHttpClient().setCookieStore(this.cookies);
    }

    public void setHeader(Header[] header) {
        this.header = header;
    }

    public CustomerHttpClient() {
    }

    /***
     * 
     * @param url
     * @param listHeader
     *            list,每行一个协议头, :号分割
     * @return
     */
    public String get(String url)
    {
        return this.get(url,null,null);
    }
    public String get(String url, List<String> listHeader,
            BasicClientCookie... cookies) {
        DefaultHttpClient client = (DefaultHttpClient) getHttpClient();
        // 新建HTTP GET
        HttpGet httpGet = new HttpGet(url);
        // 开始设置协议头
        if (null != listHeader) {
            for (String string : listHeader) {
                String[] array = string.split(",");
                if (array.length != 2) {
                    continue;
                }
                httpGet.setHeader(array[0], array[1]);
            }
        }
        if (null != cookies) {
            for (int i = 0; i < cookies.length; i++) {
                this.setCookie(cookies[i]);
            }
        }
        
         List<Cookie> list = this.getCookies().getCookies();
         for (Cookie cookie : list) {
            System.out.println(cookie.toString());
        }
        
        
        if (httpGet.getHeaders("accept").length == 0) {
            httpGet
                    .setHeader(
                            "accept","*/*");
                            //"accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
        }
        if (httpGet.getHeaders("accept-language").length == 0) {
            httpGet.setHeader("accept-language", "zh-CN,zh;q=0.8");
        }

        httpGet.setHeader("Cookie", "login=true; path=/");
        
        
        
        HttpResponse response = null;
        HttpEntity entity = null;
        String str = "";
        try {
            response = client.execute(httpGet);
            header = response.getAllHeaders();
            // 在获取文本内容
            // if(getCookies().getCookies()==null)
            // {
            //                
            // }
            // for (int i = 0; i < getCookies().getCookies().size(); i++) {
            // Cookie a = getCookies().getCookies().get(i);
            //                
            // int aa = 2;
            // }
            // System.out.println(response.getHeaders("Set-Cookie").length);
            // String a = response.getHeaders("Set-Cookie")[0].toString();
            // System.out.println(a);
            // String a1 = response.getHeaders("Set-Cookie")[1].toString();
            // String a2 = response.getHeaders("Set-Cookie")[2].toString();
            CookieStore c = this.getCookies();
            entity = response.getEntity();
            str = EntityUtils.toString(entity);
            EntityUtils.consume(entity);
            return str;
        } catch (Exception e) {
            str = "";
        } finally {
            httpGet.abort();
        }
        return "";
    }

    public String post(String url, String post, List<String> listHeader) {
        DefaultHttpClient client = (DefaultHttpClient) getHttpClient();
        HttpPost httpPost = new HttpPost(url);
        // 开始设置协议头

        // 开始设置协议头
        if (null != listHeader) {
            for (String string : listHeader) {
                String[] array = string.split(":");
                if (array.length != 2) {
                    continue;
                }
                httpPost.setHeader(array[0], array[1]);
            }
        }

        StringEntity reqEntity = null;
        ;
        HttpResponse response = null;
        HttpEntity entity = null;
        String str = "";
        try {
            reqEntity = new StringEntity(post);
            reqEntity.setContentType("application/x-www-form-urlencoded");
            httpPost.setEntity(reqEntity);
            response = client.execute(httpPost);
            header = response.getAllHeaders();
            // 在获取文本内容
            entity = response.getEntity();
            str = EntityUtils.toString(entity);
            EntityUtils.consume(entity);
            return str;
        } catch (Exception e) {
            str = "";
        } finally {
            httpPost.abort();
        }
        return "";
    }

    // public Image
    // 单例,我们需要多个浏览器,所以不要加static关键字

    public synchronized DefaultHttpClient getHttpClient() {
        // 如果等于null就创建
        if (null == customerHttpClient) {
            // 基本参数
            HttpParams params = new BasicHttpParams();
            // 设置一些基本参数
            HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
            HttpProtocolParams.setContentCharset(params, CHARSET);
            // (5)持续握手(Expect-continue handshake)
            // 在认证系统或其他可能遭到服务器拒绝应答的情况下(如:登陆失败),如果发送整个请求体,则会大大降低效率。此时,可以先发送部分请求(如:只发送请求头)进行试探,如果服务器愿意接收,则继续发送请求体。此项优化主要进行以下配置:
            HttpProtocolParams.setUseExpectContinue(params, true);
            HttpProtocolParams
                    .setUserAgent(
                            params,
                            "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0");
            HttpProtocolParams.setHttpElementCharset(params, CHARSET);
            /* 连接超时 */
            HttpConnectionParams.setConnectionTimeout(params, 2000);
            /* 发送数据超时 */
            HttpConnectionParams.setSoTimeout(params, 4000);

            // HttpClient client = new DefaultHttpClient();
            // 请求超时
            // client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,
            // 20000);
            // 读取超时
            // client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,
            // 20000 );

            // 设置我们的HttpClient支持HTTP和HTTPS两种模式,死写就是了
            SchemeRegistry schReg = new SchemeRegistry();
            schReg.register(new Scheme("http", 80, PlainSocketFactory
                    .getSocketFactory()));
            schReg.register(new Scheme("https", 443, SSLSocketFactory
                    .getSocketFactory()));
            // 使用线程安全的连接管理来创建HttpClient
            ThreadSafeClientConnManager conMgr = new ThreadSafeClientConnManager(
                    schReg);
            // 设置最大连接数
            conMgr.setMaxTotal(10000);
            // 设置每个路由默认最大连接数
            conMgr.setDefaultMaxPerRoute(100);
            // 设置代理,
            // HttpHost proxy = new HttpHost("10.36.24.3", 60001);
            // httpParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

            customerHttpClient = new DefaultHttpClient(conMgr, params);
            HttpParams params1 = customerHttpClient.getParams();
            // client.getState().setCookiePolicy(CookiePolicy.COMPATIBILITY);
            // 这种方法设置的规范只对当前的HttpState有效,参数可取值CookiePolicy.COMPATIBILITY,CookiePolicy.NETSCAPE_DRAFT或CookiePolicy.RFC2109。
            // customerHttpClient.
            // System.setProperty("apache.commons.httpclient.cookiespec",
            // "COMPATIBILITY");
            // 不允许重定向
            params1.setParameter("http.protocol.handle-redirects", false);
            params1.setParameter("http.protocol.reject-relative-redirect",
                    false);

            // 不行 params1.setParameter("accept",
            // "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");

            // params.setParameter(ClientPNames.HANDLE_REDIRECTS, false);

            // // 对每个默认的强制严格cookie策略
            customerHttpClient.getParams().setParameter(
                    ClientPNames.COOKIE_POLICY,
                    CookiePolicy.BROWSER_COMPATIBILITY);

            // // 手动管理cookie
            // customerHttpClient.getParams().setParameter(
            // ClientPNames.COOKIE_POLICY,
            // CookiePolicy.IGNORE_COOKIES);

            // HttpGet httpget = new HttpGet("http://www.broken-server.com/");
            // 对这个请求覆盖默认策略
            // httpget.getParams().setParameter(
            // ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);

            HttpConnectionParams.setConnectionTimeout(params1, 3000);
            // 可以自动重连
            HttpRequestRetryHandler requestRetryHandler2 = new HttpRequestRetryHandler() {
                // 自定义的恢复策略
                public synchronized boolean retryRequest(IOException exception,
                        int executionCount, HttpContext context) {
                    // 设置恢复策略,在发生异常时候将自动重试3次
                    if (executionCount > 3) {
                        // 超过最大次数则不需要重试
                        return false;
                    }
                    if (exception instanceof NoHttpResponseException) {
                        // 服务停掉则重新尝试连接
                        return true;
                    }
                    if (exception instanceof SSLHandshakeException) {
                        // SSL异常不需要重试
                        return false;
                    }
                    HttpRequest request = (HttpRequest) context
                            .getAttribute(ExecutionContext.HTTP_REQUEST);
                    boolean idempotent = (request instanceof HttpEntityEnclosingRequest);
                    if (!idempotent) {
                        // 请求内容相同则重试
                        return true;
                    }
                    return false;
                }
            };

            customerHttpClient
                    .setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy() {
                        @Override
                        public long getKeepAliveDuration(HttpResponse response,
                                HttpContext context) {
                            long keepAlive = super.getKeepAliveDuration(
                                    response, context);
                            if (keepAlive == -1) {
                                // 如果keep-alive值没有由服务器明确设置,那么保持连接持续5秒。
                                keepAlive = 5000;
                            }
                            return keepAlive;
                        }
                    });
            /*
             * 这个暂时不需要 DefaultHttpClient httpclient = new DefaultHttpClient();
             * httpclient
             * .removeRequestInterceptorByClass(RequestUserAgent.class);
             * httpclient.addRequestInterceptor(new HttpRequestInterceptor() {
             * public void process( HttpRequest request, HttpContext context)
             * throws HttpException, IOException {
             * request.setHeader(HTTP.USER_AGENT, "My-own-client"); } });
             */
            customerHttpClient.setHttpRequestRetryHandler(requestRetryHandler2);
            // DefaultRedirectStrategy re = new DefaultHttpClient();
            // customerHttpClient.setRedirectStrategy(re);
            customerHttpClient
                    .setRedirectStrategy(new DefaultRedirectStrategy() {
                        public boolean isRedirected(HttpRequest request,
                                HttpResponse response, HttpContext context) {
                            boolean isRedirect = false;
                            try {
                                // 重定向改为FALSE
                                isRedirect = super.isRedirected(request,
                                        response, context);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            if (!isRedirect) {
                                int responseCode = response.getStatusLine()
                                        .getStatusCode();
                                if (responseCode == 301 || responseCode == 302) {
                                    return false; // 默认不重定向
                                }
                            }
                            return isRedirect;
                        }
                    });
        }
        // httpclient.getConnectionManager().shutdown();连接不用时候关闭
        return customerHttpClient;
    }

    public String post(String url, StringEntity entity) {
        DefaultHttpClient client = (DefaultHttpClient) getHttpClient();
        //client.get
        HttpPost httpPost = new HttpPost(url);
        // 开始设置协议头
        HttpEntity entity1 = null;
        HttpResponse response = null;
        String str = "";
        try {
            Header[] hh = httpPost.getAllHeaders();
            httpPost.setEntity(entity);
            httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
            //System.out.println(entity.toString());
            //httpPost.addHeader("Content-Length", EntityUtils.toString(entity.toString()));
            
            response = client.execute(httpPost);
            header = response.getAllHeaders();
            // 在获取文本内容
            entity1 = response.getEntity();
            str = EntityUtils.toString(entity1);
            EntityUtils.consume(entity);
            return str;
        } catch (Exception e) {
            str = "";
        } finally {
            httpPost.abort();
        }
        return "";
    }
}

 

posted @ 2014-07-26 00:53  宝贝,我永远都在  阅读(324)  评论(0)    收藏  举报