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

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

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

    Java學(xué)習(xí)

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

     

    利用HttpClient獲取網(wǎng)頁(yè)內(nèi)容

    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,設(shè)置相關(guān)參數(shù)
    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;
    // 標(biāo)志初始化是否完成的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;
    }
    // 通過(guò)get方法獲取網(wǎng)頁(yè)內(nèi)容
    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);
    // 在目標(biāo)頁(yè)面情況未知的條件下,不推薦使用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 "";
    }
    }
    }


    之后,就可以通過(guò)下面的代碼獲得目標(biāo)網(wǎng)頁(yè):
    String source = HttpClientExample.getGetResponseWithHttpClient("http://www.163.com", "GBK");

    注意,在默認(rèn)情況下,HttpClient的Request的Head中
    User-Agent的值是Jakarta Commons-HttpClient 3.0RC1,如果需要改變它(例如,變?yōu)镸ozilla/4.0),必須在調(diào)用之前運(yùn)行如下語(yǔ)句:
    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 找個(gè)美女做老婆 閱讀(4523) 評(píng)論(0)  編輯  收藏


    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     

    導(dǎo)航

    統(tǒng)計(jì)

    公告

    本blog已經(jīng)搬到新家了, 新家:www.javaly.cn
     http://www.javaly.cn

    常用鏈接

    留言簿(6)

    隨筆檔案

    文章檔案

    搜索

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 亚洲欧美国产国产一区二区三区| 亚洲一区二区三区国产精品无码| 美女视频黄频a免费观看| 午夜老司机免费视频| 亚洲人成www在线播放| 久久精品免费一区二区喷潮| 亚洲18在线天美| 免费黄色大片网站| 男男gay做爽爽免费视频| 一级毛片直播亚洲| 国产免费区在线观看十分钟| 久久久久久久综合日本亚洲| 无码成A毛片免费| 亚洲国产精品久久丫| 成人免费无码大片A毛片抽搐| 日韩欧美亚洲国产精品字幕久久久| 国产亚洲精品免费| 国产成人无码免费看片软件| 亚洲成a人片在线观看无码| 午夜不卡久久精品无码免费| 亚洲一级黄色大片| 丁香亚洲综合五月天婷婷| 免费无码又爽又刺激高潮软件| 亚洲第一成年网站大全亚洲| 在线观看免费毛片| 国产精品青草视频免费播放| 亚洲性天天干天天摸| 成人黄动漫画免费网站视频| 色一情一乱一伦一视频免费看| 亚洲精品国精品久久99热一| 免费精品国产自产拍在线观看图片| 亚洲精品永久在线观看| 亚洲无线码一区二区三区| h片在线免费观看| 无人视频免费观看免费视频| 国产亚洲av片在线观看播放 | 国产成人免费a在线视频app| 一二三四在线观看免费中文在线观看 | 亚洲最大在线观看| 国产免费人成视频在线观看| 日本高清高色视频免费|