HttpClient-4.3.4使用工具類

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   os   使用   sp   

封裝HttpClient 4.3.4 ,方便日後抓取資料,資料提交(待Cookie訪問、302跳轉訪問):

package util;import java.io.IOException;import java.util.ArrayList;import java.util.List;import java.util.Map;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.http.HttpEntity;import org.apache.http.NameValuePair;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.CookieStore;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.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.message.BasicNameValuePair;import org.apache.http.util.EntityUtils;/*** 網頁抓取,httpclient-4.3.4 調用* 類名稱:HttpUtil.java* 建立時間:2014-12-1* 修改備忘:   * @version 1.0 */public class HttpUtil {    private static final Log log = LogFactory.getLog(HttpUtil.class);        public static String get(String url){        //初始化一個httpClient                /* 帶Cookie方式初始化  httpclient 作為全域變數,不要關閉        CloseableHttpClient httpclient;        CookieStore cookieStore;        String cookies ="user=155;pwd=2545454weafadfwef";        if (cookieStore==null) {            cookieStore = new BasicCookieStore();             String[] ckArr = cookies.split(";");            for (String ck : ckArr) {                String[] tempArr = ck.split("=");                if (tempArr.length==2) {                    BasicClientCookie cookie = new BasicClientCookie(tempArr[0].trim(), tempArr[1].trim());                     cookie.setVersion(1);                     cookie.setDomain("pub.alimama.com");                    cookie.setPath("/");                     cookieStore.addCookie(cookie);                 }                            }        }        httpclient =  HttpClients.custom().setDefaultCookieStore(cookieStore).build();         */                    //普通方式構造 httpclient        CloseableHttpClient httpclient = HttpClients.createDefault();        return get(url,httpclient);    }        /**     * get方式擷取網頁內容     * @param url  網址     * @param httpclient  httpclient 執行個體     * @param referer  構造來源地址     * @return     */    public static String get(String url,CloseableHttpClient httpclient,String referer){        String html ="";        try {            HttpGet httpGet = new HttpGet(url);            Integer timeOut = 30000; //30 秒 逾時            RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeOut) .setConnectTimeout(timeOut).setSocketTimeout(timeOut).build();            httpGet.setConfig(requestConfig);            if (referer!=null) {                httpGet.setHeader("referer", referer);            }            CloseableHttpResponse response = null;            try {                response = httpclient.execute(httpGet);                HttpEntity entity = response.getEntity();                html = EntityUtils.toString(entity);            } catch (ClientProtocolException e) {                log.error("http協議異常:"+e.getMessage());            } catch (IOException e) {                log.error("http讀取異常:"+e.getMessage());            }catch (Exception e) {                log.error("httpException:"+e.getMessage());            }finally {                 try {                    response.close();                } catch (IOException e) {                    log.error("http輸出資料流關閉錯誤:"+e.getMessage());                }            }        } finally {        }        return html;    }        /**     * get方式擷取網頁內容     * @param url  網址     * @param httpclient  httpclient 執行個體     * @return     */    public static String get(String url,CloseableHttpClient httpclient){        return get(url, httpclient, null);    }        /**     * post方式提交資料,擷取網頁內容     * @param url  網址     * @param parmas  Map<String, String>方式存放資料名稱和值     * @param httpclient  httpclient 執行個體     * @param referer  構造來源地址     * @return     */    public static String post(String url, Map<String, String> parmas,CloseableHttpClient httpclient,String referer){        String html ="";        try {            HttpPost httpPost = new HttpPost(url);            Integer timeOut = 30000; //30 秒 逾時            RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeOut) .setConnectTimeout(timeOut).setSocketTimeout(timeOut).build();            httpPost.setConfig(requestConfig);            if (referer!=null) {                httpPost.setHeader("referer", referer);            }            CloseableHttpResponse response = null;            try {                //參數設定                List <NameValuePair> nvps = new ArrayList <NameValuePair>();                for (String key : parmas.keySet()) {                    nvps.add(new BasicNameValuePair(key, parmas.get(key)));                }                httpPost.setEntity(new UrlEncodedFormEntity(nvps));                response = httpclient.execute(httpPost);                HttpEntity entity = response.getEntity();                html = EntityUtils.toString(entity);            } catch (ClientProtocolException e) {                log.error("http協議異常:"+e.getMessage());            } catch (IOException e) {                log.error("http讀取異常:"+e.getMessage());            }catch (Exception e) {                log.error("httpException:"+e.getMessage());            }finally {                 try {                    response.close();                } catch (IOException e) {                    log.error("http輸出資料流關閉錯誤:"+e.getMessage());                }            }        } finally {                    }        return html;    }        /**     * post方式提交資料,擷取網頁內容     * @param url  網址     * @param parmas  Map<String, String>方式存放資料名稱和值     * @param httpclient  httpclient 執行個體     * @return     */    public static String post(String url, Map<String, String> parmas,CloseableHttpClient httpclient){        return post(url, parmas, httpclient, null);    }            /**     * 簡單方式,post方式提交資料,擷取網頁內容     * @param url  網址     * @param parmas  Map<String, String>方式存放資料名稱和值     * @return     */    public static String post(String url, Map<String, String> parmas){        CloseableHttpClient httpclient = HttpClients.createDefault();        return post(url, parmas,httpclient);    }        /**     * 302連續跳轉,擷取真實串連     * @param url     * @param cookieStore     * @return     */    public static String get302(String url,CookieStore cookieStore) {        CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();        HttpGet httpget = new HttpGet(url);        Integer timeOut = 20000; //30 秒 逾時        RequestConfig requestConfig = RequestConfig.custom().setRedirectsEnabled(false).setCircularRedirectsAllowed(false).setConnectionRequestTimeout(timeOut) .setConnectTimeout(timeOut).setSocketTimeout(timeOut).build();        httpget.setConfig(requestConfig);        CloseableHttpResponse response = null;        try {            response = httpclient.execute(httpget);            int status = response.getStatusLine().getStatusCode();            if (status==302) {                url =  response.getFirstHeader("Location").getValue();                return get302(url,cookieStore);            }else {                 HttpEntity entity = response.getEntity();                 return EntityUtils.toString(entity);            }        } catch (Exception e) {            System.err.println("擷取失敗");        } finally {            try {                response.close();                httpclient.close();            } catch (IOException e) {}        }        return null;    }}

 

HttpClient-4.3.4使用工具類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.