<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協(xié)議是目前互聯(lián)網(wǎng)上最重要的協(xié)議,許多軟件與服務(wù)都需要依賴HTTP協(xié)議。
    雖然java.net這個(gè)package中包含了對(duì)HTTP的基本支持,但還有很多高級(jí)和復(fù)雜的功能無法實(shí)現(xiàn),這不能不說是一個(gè)遺憾。
    JAVA機(jī)網(wǎng)[www.cnjm.net]
    HttpClient作為Apache的開源項(xiàng)目項(xiàng)目之一,為基于HTTP協(xié)議的操作提供了強(qiáng)大的客戶端執(zhí)行支持,最新的版本為3.0RC3。
    下面通過一個(gè)例子簡要展示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,設(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;
    }

    //通過get方法獲取網(wǎng)頁內(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)頁面情況未知的條件下,不推薦使用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 "";
    }
    }
    }
    之后,就可以通過下面的代碼獲得目標(biāo)網(wǎng)頁:
    String source = HttpClientExample.getGetResponseWithHttpClient("www.sina.com.cn", "GBK");

    注意,在默認(rèn)情況下,HttpClient的Request的Head中
    JAVA手機(jī)網(wǎng)[www.cnjm.net]
    User-Agent的值是Jakarta Commons-HttpClient 3.0RC1,如果需要改變它(例如,變?yōu)镸ozilla/4.0),必須在調(diào)用之前運(yùn)行如下語句:
    System.getProperties().setProperty("httpclient.useragent", "Mozilla/4.0");
    來自:http://www.cnjm.net/tech/article1153.html
    posted on 2009-06-23 17:56 SIMONE 閱讀(1222) 評(píng)論(0)  編輯  收藏 所屬分類: JAVA
    主站蜘蛛池模板: 特黄特色大片免费| 亚洲AV无码专区国产乱码4SE| 色窝窝免费一区二区三区| 精品熟女少妇av免费久久| 日本免费人成网ww555在线| 国内永久免费crm系统z在线| 国产无遮挡又黄又爽免费网站| 9久久免费国产精品特黄| aaa毛片视频免费观看| 最近免费字幕中文大全| 国产在线观a免费观看| 国内精品免费久久影院| 免费无码黄网站在线看| 日韩免费视频一区二区| 日本免费一区二区三区四区五六区 | 国产中文字幕在线免费观看| 国产自国产自愉自愉免费24区| 成人影片一区免费观看| 日韩精品免费视频| 中文字幕亚洲免费无线观看日本| 99精品免费观看| 全免费毛片在线播放| 在线免费观看一级片| 成人亚洲网站www在线观看| 久久久久无码专区亚洲av| 国产亚洲A∨片在线观看| 亚洲综合在线观看视频| 久久夜色精品国产噜噜亚洲a| 国产亚洲欧美在线观看| a一级爱做片免费| 日韩午夜理论免费TV影院| 久九九精品免费视频| 国产美女无遮挡免费视频网站 | 成年女人免费视频播放体验区| 国产成人综合久久精品免费| 亚洲午夜AV无码专区在线播放| 亚洲av无码专区国产乱码在线观看| 亚洲国产高清在线精品一区 | 亚洲丝袜美腿视频| 亚洲午夜在线播放| 一区二区免费电影|