默認上下文由您正在使用的HttpClient
實現進行配置。對於基於AbstractHttpClient
的實現,該工作由createHttpContext()
方法完成。請注意,每個execute
呼叫都會創建一個新的默認上下文。
自己配置默認上下文的一種方法是擴展現有的HttpClient
實現類之一併覆蓋該方法。
另一種方法是設置該方法使用的各種參數;例如連接管理器的方案註冊表,authScheme註冊表,cookieSpecs註冊表,cookie存儲或憑證提供程序。
爲了記錄在案,這裏的DefaultHttpClient.createHttpContext()
做什麼:
@Override
protected HttpContext createHttpContext() {
HttpContext context = new BasicHttpContext();
context.setAttribute(
ClientContext.SCHEME_REGISTRY,
getConnectionManager().getSchemeRegistry());
context.setAttribute(
ClientContext.AUTHSCHEME_REGISTRY,
getAuthSchemes());
context.setAttribute(
ClientContext.COOKIESPEC_REGISTRY,
getCookieSpecs());
context.setAttribute(
ClientContext.COOKIE_STORE,
getCookieStore());
context.setAttribute(
ClientContext.CREDS_PROVIDER,
getCredentialsProvider());
return context;
}
我試圖默認AUTH_CACHE,以便所有請求預先授權basic-auth標頭(因爲這是一個僅通過https連接到已知服務器的REST客戶端): localcontext.setAttribute(ClientContext.AUTH_CACHE,authCache) ; 我沒有看到有沒有設置我自己的默認上下文的權利? – 2011-05-23 13:20:29
你是對的,我沒有看到任何其他的方式去做你需要的。 @Stephen C建議子類化現有的'HttpClient'實現將是當前API的唯一途徑。 – laz 2011-05-23 14:22:03