<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 閱讀(1222) 評論(0)  編輯  收藏 所屬分類: JAVA
    主站蜘蛛池模板: 日本视频免费在线| 成年人免费视频观看| 国产成人A人亚洲精品无码| 国产V片在线播放免费无码| 亚洲国产成人影院播放| 男女作爱免费网站| 亚洲乱码日产精品a级毛片久久| 国产尤物在线视精品在亚洲| 亚洲国产成人久久精品99 | 亚洲高清视频免费| 波多野结衣在线免费观看| 亚洲av日韩av无码| 久久久久久亚洲AV无码专区| 国产精品亚洲四区在线观看| 成年男女免费视频网站| 亚洲成a∨人片在无码2023| 国产免费一区二区三区VR| 一区二区在线视频免费观看| 亚洲人成精品久久久久| 亚洲电影免费观看| 亚洲精品人成网线在线播放va| 免费在线观看你懂的| 国产猛男猛女超爽免费视频| 久久亚洲私人国产精品| 成人无遮挡毛片免费看| 国产亚洲精品免费| 亚洲av伊人久久综合密臀性色| www视频免费看| 深夜a级毛片免费无码| 亚洲成AV人在线观看天堂无码| 99久久这里只精品国产免费| 免费无码又爽又黄又刺激网站| 亚洲精品狼友在线播放| 野花高清在线电影观看免费视频| 日韩亚洲翔田千里在线| 亚洲精品人成在线观看| 国产成人免费网站在线观看| 国产成人免费AV在线播放| 亚洲日韩国产AV无码无码精品 | 日本无卡码免费一区二区三区| 国产vA免费精品高清在线观看|