<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Java學習

    java,spring,structs,hibernate,jsf,ireport,jfreechart,jasperreport,tomcat,jboss -----本博客已經搬家了,新的地址是 http://www.javaly.cn 如果有對文章有任何疑問或者有任何不懂的地方,歡迎到www.javaly.cn (Java樂園)指出,我會盡力幫助解決。一起進步

     

    利用HttpClient獲取網頁內容

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.UnsupportedEncodingException;
    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
    import org.apache.commons.httpclient.NameValuePair;
    import org.apache.commons.httpclient.methods.GetMethod;
    import org.apache.commons.httpclient.methods.PostMethod;
    public class HttpClientExample {
    // 獲得ConnectionManager,設置相關參數
    private static MultiThreadedHttpConnectionManager manager = new MultiThreadedHttpConnectionManager();
    private static int connectionTimeOut = 20000;
    private static int socketTimeOut = 10000;
    private static int maxConnectionPerHost = 5;
    private static int maxTotalConnections = 40;
    // 標志初始化是否完成的flag
    private static boolean initialed = false;
    // 初始化ConnectionManger的方法
    public static void SetPara() {
    manager.getParams().setConnectionTimeout(connectionTimeOut);
    manager.getParams().setSoTimeout(socketTimeOut);
    manager.getParams().setDefaultMaxConnectionsPerHost(
    maxConnectionPerHost);
    manager.getParams().setMaxTotalConnections(maxTotalConnections);
    initialed = true;
    }
    // 通過get方法獲取網頁內容
    public static String getGetResponseWithHttpClient(String url, String encode) {
    HttpClient client = new HttpClient(manager);
    if (initialed) {
    HttpClientExample.SetPara();
    }
    GetMethod get = new GetMethod(url);
    get.setFollowRedirects(true);
    String result = null;
    StringBuffer resultBuffer = new StringBuffer();
    try {
    client.executeMethod(get);
    // 在目標頁面情況未知的條件下,不推薦使用getResponseBodyAsString()方法
    // String strGetResponseBody = post.getResponseBodyAsString();
    BufferedReader in = new BufferedReader(new InputStreamReader(get
    .getResponseBodyAsStream(), get.getResponseCharSet()));
    String inputLine = null;
    while ((inputLine = in.readLine()) != null) {
    resultBuffer.append(inputLine);
    resultBuffer.append("\n");
    }
    in.close();
    result = resultBuffer.toString();
    // iso-8859-1 is the default reading encode
    result = HttpClientExample.ConverterStringCode(resultBuffer
    .toString(), get.getResponseCharSet(), encode);
    } catch (Exception e) {
    e.printStackTrace();
    result = "";
    } finally {
    get.releaseConnection();
    return result;
    }
    }
    public static String getPostResponseWithHttpClient(String url, String encode) {
    HttpClient client = new HttpClient(manager);
    if (initialed) {
    HttpClientExample.SetPara();
    }
    PostMethod post = new PostMethod(url);
    post.setFollowRedirects(false);
    StringBuffer resultBuffer = new StringBuffer();
    String result = null;
    try {
    client.executeMethod(post);
    BufferedReader in = new BufferedReader(new InputStreamReader(post
    .getResponseBodyAsStream(), post.getResponseCharSet()));
    String inputLine = null;
    while ((inputLine = in.readLine()) != null) {
    resultBuffer.append(inputLine);
    resultBuffer.append("\n");
    }
    in.close();
    // iso-8859-1 is the default reading encode
    result = HttpClientExample.ConverterStringCode(resultBuffer
    .toString(), post.getResponseCharSet(), encode);
    } catch (Exception e) {
    e.printStackTrace();
    result = "";
    } finally {
    post.releaseConnection();
    return result;
    }
    }
    public static String getPostResponseWithHttpClient(String url,
    String encode, NameValuePair[] nameValuePair) {
    HttpClient client = new HttpClient(manager);
    if (initialed) {
    HttpClientExample.SetPara();
    }
    PostMethod post = new PostMethod(url);
    post.setRequestBody(nameValuePair);
    post.setFollowRedirects(false);
    String result = null;
    StringBuffer resultBuffer = new StringBuffer();
    try {
    client.executeMethod(post);
    BufferedReader in = new BufferedReader(new InputStreamReader(post
    .getResponseBodyAsStream(), post.getResponseCharSet()));
    String inputLine = null;
    while ((inputLine = in.readLine()) != null) {
    resultBuffer.append(inputLine);
    resultBuffer.append("\n");
    }
    in.close();
    // iso-8859-1 is the default reading encode
    result = HttpClientExample.ConverterStringCode(resultBuffer
    .toString(), post.getResponseCharSet(), encode);
    } catch (Exception e) {
    e.printStackTrace();
    result = "";
    } finally {
    post.releaseConnection();
    return result;
    }
    }
    private static String ConverterStringCode(String source, String srcEncode,
    String destEncode) {
    if (source != null) {
    try {
    return new String(source.getBytes(srcEncode), destEncode);
    } catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    return "";
    }
    } else {
    return "";
    }
    }
    }


    之后,就可以通過下面的代碼獲得目標網頁:
    String source = HttpClientExample.getGetResponseWithHttpClient("http://www.163.com", "GBK");

    注意,在默認情況下,HttpClient的Request的Head中
    User-Agent的值是Jakarta Commons-HttpClient 3.0RC1,如果需要改變它(例如,變為Mozilla/4.0),必須在調用之前運行如下語句:
    System.getProperties().setProperty("httpclient.useragent", "Mozilla/4.0");
    zhuan:http://www.diybl.com/course/3_program/java/javajs/20090210/155102.html

    posted on 2009-04-28 09:28 找個美女做老婆 閱讀(4523) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     

    導航

    統計

    公告

    本blog已經搬到新家了, 新家:www.javaly.cn
     http://www.javaly.cn

    常用鏈接

    留言簿(6)

    隨筆檔案

    文章檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 内射无码专区久久亚洲| 两个人看的www高清免费观看| 曰曰鲁夜夜免费播放视频| 亚洲成A人片在线观看WWW| 国产免费高清69式视频在线观看| 免费观看黄网站在线播放| 91精品免费观看| 国产成人高清精品免费软件| 亚洲AV成人无码天堂| 黄页网站在线看免费| 亚洲香蕉久久一区二区| 操美女视频免费网站| 亚洲国产日韩a在线播放| 尤物永久免费AV无码网站| 久久人午夜亚洲精品无码区| 免费真实播放国产乱子伦| 国产成人亚洲精品无码AV大片| yy6080久久亚洲精品| 两性色午夜视频免费播放| 国产精品亚洲а∨无码播放| 免费一区二区三区| 亚洲五月六月丁香激情| 91频在线观看免费大全| 精品韩国亚洲av无码不卡区| 免费大黄网站在线观看| 香蕉视频亚洲一级| 亚洲偷自拍拍综合网| 成人性生交大片免费看中文| 久久精品国产亚洲AV嫖农村妇女| 最近高清中文字幕无吗免费看| 中文字幕亚洲综合小综合在线| 免费观看成人毛片a片2008| 男人的天堂av亚洲一区2区| 人人狠狠综合久久亚洲高清| 日韩精品无码免费专区午夜| 亚洲色图在线播放| 久久国内免费视频| caoporm碰最新免费公开视频| 91精品国产亚洲爽啪在线观看| 免费高清资源黄网站在线观看 | 亚洲五月综合缴情婷婷|