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

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

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

    風行天下

    JAVA太極
    posts - 4, comments - 10, trackbacks - 0, articles - 55
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    Jakarta Commons HttpClient

    Posted on 2005-04-04 11:18 風太少 閱讀(739) 評論(0)  編輯  收藏
    SECTION 01 HttpClient 總覽

    Hyper-Text Transfer Protocol (HTTP) 是現在網絡上最通行的通訊協議, 隨著 Webservice 的技術快速發展, 也讓許多的應用程序結合 HTTP 擴展更大的功能, 或許你會說 java.net 不是已經有基本的存取 HTTP 的 method 了, 為何還要有這個 commons 的項目呢, 請容許我慢慢說明 ~~


    binary 下載處 http://jakarta.apache.org/builds/jakarta-commons/release/commons-httpclient/v2.0/

    source 下載處 http://www.apache.org/dist/jakarta/commons/httpclient/source/


    SECTION 02 特色

    是因為他的特色..... 有許多是 java.net.* 沒有提供的, 或者自己要實現的. 不如就直接采用 commons-httpclient.
    • 使用 Pure Java 開發標準的 HTTP v1.0 及 v1.1
    • 實現所有的 HTTP methods (GET, POST, PUT, DELETE,HEAD, OPTIONS, and TRACE)
    • 支持 HTTPS 的協議
    • 支持 proxies 的各種情況
    • 利用 Multi Form 上傳文件
    • 支持認證機制
    • 可以設置最大連結數量
    • 自動的 cookie 處理模式
    • Request 及 Response 最佳化處理
    • 支持 HTTP 1.0 KeepAlive 聯機模式及 1.1 的 persistance 保存狀態
    • 直接存取服務器送來的 response code 及 header
    • 能夠設置連結超時
    • 實現 Command Pattern 允許去平行處理及有效重復使用連結.
    • 這個是開放源碼的



    SECTION 03 依循自

    Commons-HttpClient 是遵循了下面的 spec ( Internet Engineering Task Force (IETF) )
    • RFC1945Hypertext Transfer Protocol -- HTTP/1.0
    • RFC2616Hypertext Transfer Protocol -- HTTP/1.1
    • RFC2617HTTP Authentication: Basic and Digest Access Authentication
    • RFC2109HTTP State Management Mechanism (Cookies)
    • RFC2396Uniform Resource Identifiers (URI): Generic Syntax
    • RFC1867Form-based File Upload in HTML



    SECTION 04 與其它的 HttpClient 比較

    此來源出自 http://www.nogoop.com/product_16.html#compare
    nogoop Sun innovation Jakarta
    Features
    cookies X X
    plug compatible X X X [partial]
    true request output stream X X
    true response input stream X X X
    connection keep alive X X X X
    connection pool throttling X X
    connection/request timeout X X X
    idle connection timeout X
    pipelining of requests X
    SSL X X X X
    basic authentication X X X X
    digest authentication X X X X
    NTLM authentication X X
    proxy authentication X X X X
    Support
    minimum JRE version 1.2 1.0 1.2 1.2
    price $399 free free free
    source available X X X
    diagnostic tracing X X X
    actively supported X X X
    fix turnaround fast slow none medium
    license purchase Sun JRE LGPL Apache



    SECTION 05 簡單范例

    基本上 HttpClient 需要 commons-logging, 所以你要在 classpath 設置這兩個 jar 文件, 接著你要了解 HTTP 通訊的方式, 標準步驟如下..
    1. 建立 HttpClient 的 instance ( HttpClient client = new HttpClient(); )
    2. 建立 HttpClient instance 要使用的 method ( 例如 GET/POST 等等 連結到一個 URL , HttpMethod method = new GetMethod("http://www.apache.org/"); )
    3. 告訴 HttpClient 去執行這個訊息 ( statusCode = client.executeMethod(method); )
    4. 得到 Server 端的響應 ( byte[] responseBody = method.getResponseBody(); )
    5. 結束這個連結 ( method.releaseConnection(); )
    6. 處理這個響應數據 ( System.err.println(new String(responseBody)); )
    
    import org.apache.commons.httpclient.*;
    import org.apache.commons.httpclient.methods.*;
    import java.io.*;
    
    public class HttpClientTutorial {
      
      private static String url = "http://www.apache.org/";
    
      public static void main(String[] args) {
    	// Create an instance of HttpClient.
    	HttpClient client = new HttpClient();
    
    	// Create a method instance.
    	HttpMethod method = new GetMethod(url);
    
    	// Execute the method.
    	int statusCode = -1;
    	int attempt = 0;
    	// We will retry up to 3 times.
    	while (statusCode == -1 && attempt < 3) {
    	  try {
    		// execute the method.
    		statusCode = client.executeMethod(method);
    	  } catch (HttpRecoverableException e) {
    		System.err.println(
    		  "A recoverable exception occurred, retrying." + 
    		  e.getMessage());
    	  } catch (IOException e) {
    		System.err.println("Failed to download file.");
    		e.printStackTrace();
    		System.exit(-1);
    	  }
    	}
    	// Check that we didn't run out of retries.
    	if (statusCode == -1) {
    	  System.err.println("Failed to recover from exception.");
    	  System.exit(-2);
    	}
    
    
    	// Read the response body.
    	byte[] responseBody = method.getResponseBody();
    
    	// Release the connection.
    	method.releaseConnection();
    
    	// Deal with the response.
    	// Use caution: ensure correct character encoding and is not binary data
    	System.err.println(new String(responseBody));
      }
    }
    

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


    網站導航:
     
    主站蜘蛛池模板: 亚洲男女一区二区三区| 亚洲AV综合色区无码另类小说 | 国产精品亚洲片在线观看不卡 | 亚洲国产亚洲片在线观看播放| 无码人妻AV免费一区二区三区| 国产亚洲av片在线观看播放| 在线观看片免费人成视频无码| 亚洲精品成人片在线播放| 久草福利资源网站免费| 亚洲网站视频在线观看| 999在线视频精品免费播放观看| 亚洲AV无码精品蜜桃| 天天摸天天碰成人免费视频| 自拍偷自拍亚洲精品偷一| 亚洲综合另类小说色区| 免费看无码特级毛片| 亚洲欧洲日本国产| 啦啦啦手机完整免费高清观看 | 亚洲精品国产福利在线观看| 日本免费网址大全在线观看| 亚洲国产精品无码中文lv| 亚洲国产精品一区二区第四页| 国内少妇偷人精品视频免费| 亚洲国产夜色在线观看| 国产精品公开免费视频| 两个人看的www高清免费视频| 亚洲韩国在线一卡二卡| 男女交性永久免费视频播放| 国产精品免费久久久久久久久| 91情国产l精品国产亚洲区| 在线精品免费视频无码的| 一级毛片正片免费视频手机看| 亚洲VA中文字幕不卡无码| 丁香花在线观看免费观看| 成人久久久观看免费毛片| 中文字幕亚洲第一在线| 亚洲国产av无码精品| 18禁美女黄网站色大片免费观看 | 四虎永久在线观看免费网站网址| 国产成人高清亚洲一区91| 久久久亚洲欧洲日产国码农村|