<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # httpclient5 <div style="font-size:large;font-weight:bold;"><span style="font-size:19px;">第五章 HTTP客戶端服務</span></div><div style="font-size:9.5pt;"><h3>5.1 HttpClient門面</h3><p style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">HttpClient接口代表了最重要的HTTP請求執行的契約。它沒有在請求執行處理上強加限制或特殊細節,而在連接管理,狀態管理,認證和處理重定向到具體實現上留下了細節。這應該使得很容易使用額外的功能,比如響應內容緩存來裝飾接口。</p><p style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">DefaultHttpClient 是HttpClient接口的默認實現。這個類扮演了很多特殊用戶程序或策略接口實現負責處理特定HTTP協議方面,比如重定向到處理認證或做出關于連接 持久化和保持活動的持續時間決定的門面。這使得用戶可以選擇使用定制,具體程序等來替換某些方面默認實現。</p><blockquote style="background-color:#F9F9FF;"><div style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">DefaultHttpClient httpclient = new DefaultHttpClient();</div><div style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">httpclient.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy() {</div><div style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">@Override</div><div style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">public long getKeepAliveDuration(HttpResponse response,</div><div style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">HttpContext context) {</div><div style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">long keepAlive = super.getKeepAliveDuration(response, context);</div><div style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">if (keepAlive == -1) {</div><div style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">// 如果keep-alive值沒有由服務器明確設置,那么保持連接持續5秒。</div><div style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">keepAlive = 5000;</div><div style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">}</div><div style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">return keepAlive;</div><div style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">}</div><div style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">});</div></blockquote><p style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">DefaultHttpClient 也維護一組協議攔截器,意在處理即將離開的請求和即將到達的響應,而且提供管理那些攔截器的方法。新的協議攔截器可以被引入到協議處理器鏈中,或在需要時 從中移除。內部的協議攔截器存儲在一個簡單的java.util.ArrayList中。它們以被加入到list中的自然順序來執行。</p><blockquote style="background-color:#F9F9FF;"><div style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">DefaultHttpClient httpclient = new DefaultHttpClient();</div><div style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">httpclient.removeRequestInterceptorByClass(RequestUserAgent.class);</div><div style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">httpclient.addRequestInterceptor(new HttpRequestInterceptor() {</div><div style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">public void process(</div><div style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">HttpRequest request, HttpContext context)</div><div style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">throws HttpException, IOException {</div><div style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">request.setHeader(HTTP.USER_AGENT, "My-own-client");</div><div style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">}</div><div style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">});</div></blockquote><p style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">DefaultHttpClient 是線程安全的。建議相同的這個類的實例被重用于多個請求的執行。當一個DefaultHttpClient實例不再需要而且要脫離范圍時,和它關聯的連接 管理器必須調用ClientConnectionManager#shutdown()方法關閉。</p><blockquote style="background-color:#F9F9FF;"><div style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">HttpClient httpclient = new DefaultHttpClient();</div><div style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">// 做些有用的事</div><div style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">httpclient.getConnectionManager().shutdown();</div></blockquote><h3>5.2 HttpClient參數</h3><div style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">這些是可以用于定制默認HttpClient實現行為的參數:</div><ul><li>'http.protocol.handle-redirects':定義了重定向是否應該自動處理。這個參數期望得到一個java.lang.Boolean類型的值。如果這個參數沒有被設置,HttpClient將會自動處理重定向。</li><li>'http.protocol.reject- relative-redirect':定義了是否相對的重定向應該被拒絕。HTTP規范需要位置值是一個絕對URI。這個參數期望得到一個 java.lang.Boolean類型的值。如果這個參數沒有被設置,那么就允許相對重定向。</li><li>'http.protocol.max- redirects':定義了要遵循重定向的最大數量。這個重定向數字的限制意在防止由破碎的服務器端腳本引發的死循環。這個參數期望得到一個 java.lang.Integer類型的值。如果這個參數沒有被設置,那么只允許不多余100次重定向。</li><li>'http.protocol.allow- circular-redirects':定義環形重定向(重定向到相同路徑)是否被允許。HTTP規范在環形重定向沒有足夠清晰的允許表述,因此這作為 可選的是可以開啟的。這個參數期望得到一個java.lang.Boolean類型的值。如果這個參數沒有被設置,那么環形重定向就不允許。</li><li>'http.connection- manager.factory-class-name':定義了默認的ClientConnectionManager實現的類型。這個參數期望得到一 個java.lang.String類型的值。如果這個參數沒有被設置,對于每個默認的將使用SingleClientConnManager。</li><li>'http.virtual-host':定義了在頭部信息Host中使用的虛擬主機名稱,來代替物理主機名稱。這個參數期望得到一個HttpHost類型的值。如果這個參數沒有被設置,那么將會使用目標主機的名稱或IP地址。</li><li>'http.default-headers':定義了每次請求默認發送的頭部信息。這個參數期望得到一個包含Header對象的java.util.Collection類型值。</li><li>'http.default-host':定義了默認主機。如果目標主機沒有在請求URI(相對URI)中明確指定,那么就使用默認值。這個參數期望得到一個HttpHost類型的值。</li></ul><h3>5.3 自動重定向處理</h3><p style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">HttpClient處理所有類型的自動重定向,除了那些由HTTP規范明令禁止的,比如需要用戶干預的。參考其它(狀態碼303)POST和PUT請求重定向轉換為由HTTP規范需要的GET請求。</p><h3>5.4 HTTP客戶端和執行上下文</h3><p style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">DefaultHttpClient 將HTTP請求視為不變的對象,也從來不會假定在請求執行期間改變。相反,它創建了一個原請求對象私有的可變副本,副本的屬性可以基于執行上下文來更新。 因此,如目標主鍵和請求URI的final類型的請求參數可以在請求執行之后,由檢查本地HTTP上下文來決定。</p><blockquote style="background-color:#F9F9FF;"><div style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">DefaultHttpClient httpclient = new DefaultHttpClient();</div><div style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">HttpContext localContext = new BasicHttpContext();</div><div style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">HttpGet httpget = new HttpGet("http://localhost:8080/");</div><div style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">HttpResponse response = httpclient.execute(httpget, localContext);</div><div style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">HttpHost target = (HttpHost) localContext.getAttribute(</div><div style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">ExecutionContext.HTTP_TARGET_HOST);</div><div style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">HttpUriRequest req = (HttpUriRequest) localContext.getAttribute(</div><div style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">ExecutionContext.HTTP_REQUEST);</div><div style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">System.out.println("Target host: " + target);</div><div style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">System.out.println("Final request URI: " + req.getURI());</div><div style="color:inherit;font-family:inherit;font-size:inherit;font-style:inherit;">System.out.println("Final request method: " + req.getMethod());</div></blockquote></div>
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看