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

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

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

    posts - 495,comments - 227,trackbacks - 0
    HTTP協議是目前互聯網上最重要的協議,許多軟件與服務都需要依賴HTTP協議。
    雖然java.net這個package中包含了對HTTP的基本支持,但還有很多高級和復雜的功能無法實現,這不能不說是一個遺憾。
    JAVA機網[www.cnjm.net]
    HttpClient作為Apache的開源項目項目之一,為基于HTTP協議的操作提供了強大的客戶端執行支持,最新的版本為3.0RC3。
    下面通過一個例子簡要展示HttpClient的使用方法:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;

    iimport java.io.UnsupportedEncodingException;

    import java.util.*;

    import org.apache.commons.httpclient.Header;
    import org.apache.commons.httpclient.HostConfiguration;
    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.HttpConnection;
    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;


    /**
    @author steven
    */
    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 (src != null) {
    try {

    return new String(src.getBytes(srcEncode), destEncode);
    catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    return "";
    }

    else {
    return "";
    }
    }
    }
    之后,就可以通過下面的代碼獲得目標網頁:
    String source = HttpClientExample.getGetResponseWithHttpClient("www.sina.com.cn", "GBK");

    注意,在默認情況下,HttpClient的Request的Head中
    JAVA手機網[www.cnjm.net]
    User-Agent的值是Jakarta Commons-HttpClient 3.0RC1,如果需要改變它(例如,變為Mozilla/4.0),必須在調用之前運行如下語句:
    System.getProperties().setProperty("httpclient.useragent", "Mozilla/4.0");
    來自:http://www.cnjm.net/tech/article1153.html
    posted on 2009-06-23 17:56 SIMONE 閱讀(1233) 評論(0)  編輯  收藏 所屬分類: JAVA
    主站蜘蛛池模板: 国产精彩免费视频| 黄页网站在线观看免费| 毛片在线播放免费观看| 亚洲国产精品一区二区第一页免 | 久久精品国产亚洲AV香蕉| 成人免费av一区二区三区| 亚洲午夜久久久影院| 久久久久女教师免费一区| 伊人亚洲综合青草青草久热| 国产精品九九久久免费视频| 久久久久亚洲av成人无码电影| 特a级免费高清黄色片| 亚洲午夜爱爱香蕉片| 中文字幕不卡免费视频| 国产V亚洲V天堂无码久久久| 18pao国产成视频永久免费| 亚洲va无码va在线va天堂| 日本黄色动图免费在线观看| 久久精品国产精品亚洲艾草网| 久久久久免费看成人影片| 亚洲国产精品成人综合久久久| 国产福利在线免费| 亚洲国产成人久久精品大牛影视| 免费一区二区视频| 精品久久久久久国产免费了| 亚洲av无码一区二区三区不卡| 97在线视频免费播放| 亚洲日韩久久综合中文字幕| 亚洲成a人片在线观看久| 二个人看的www免费视频| 78成人精品电影在线播放日韩精品电影一区亚洲| 久久精品无码专区免费青青| 亚洲偷自精品三十六区| 亚洲AV永久无码精品一区二区国产 | 久久亚洲综合色一区二区三区 | 色www免费视频| 久久精品国产96精品亚洲| 日韩免费一区二区三区在线| 国产综合成人亚洲区| 亚洲AV无码国产丝袜在线观看| 可以免费看黄的网站|