httpclient notes

http提交form數據參考:The enctype attribute of the FORM element specifies the content type used to encode the form data set for submission to the server. User agents must support the content types listed below. Behavior for other content types is unspecified.

http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1

 

http post數據的時候還需要考慮無key的情況,所以requst方法函數在RequestMothod爲POST的時候,需要對傳入參數加個判斷進行處理

 

《httpclient-tutorial-simplified-chinese》1.3.4 請求重試處理:爲了開啓自定義異常恢復機制,應該提供一個HttpRequestRetryHandler接口的實現。

import java.io.IOException;

import javax.net.ssl.SSLHandshakeException;

import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpRequest;
import org.apache.http.NoHttpResponseException;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;

public class HttpRequestRetryHandler implements
        org.apache.http.client.HttpRequestRetryHandler {
        
        int _retries;
        
        public HttpRequestRetryHandler(int retries) {
                _retries = retries;
        }

    public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
        if (executionCount >= _retries) {
            // Do not retry if over max retry count
            return false;
        }
        if (exception instanceof NoHttpResponseException) {
            // Retry if the server dropped connection on us
            return true;
        }
        if (exception instanceof SSLHandshakeException) {
            // Do not retry on SSL handshake exception
            return false;
        }
        HttpRequest request = (HttpRequest) context.getAttribute(
                ExecutionContext.HTTP_REQUEST);
        boolean idempotent = !(request instanceof HttpEntityEnclosingRequest); 
        if (idempotent) {
            // Retry if the request is considered idempotent 
            return true;
        }
        return false;
    }

}
 HTTP/1.1 明確地定義了冪等的方法,描述如下[方法也可以有“冪等”屬性在那些(除了錯誤或過期問題)N的副作用>0的相同請求和獨立的請求是相同的]換句話說,應用程序應該保證準備着來處理多個相同方法執行的實現。這是可以達到的,比如,通過提供一個獨立的事務ID和其它避免執行相同邏輯操作的方法。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章