[英]cookie persistence with DefaultHttpClient and Swingworker
[英]using retrofit with Cookie persistence
我們,我正在使用改造,我想知道如何透明地處理會話cookie。 為此我擴展了給定的ApacheClient並在自定義調用ApacheClient.execute(HttpClient,HttpUriRequest)中使用CookieStore:
Client client = new ApacheClient() {
final CookieStore cookieStore = new BasicCookieStore();
@Override
protected HttpResponse execute(HttpClient client, HttpUriRequest request) throws IOException {
// BasicHttpContext is not thread safe
// CookieStore is thread safe
BasicHttpContext httpContext = new BasicHttpContext();
httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
return client.execute(request, httpContext);
}
};
RestAdapter restAdapter = new RestAdapter.Builder()
.setServer(API_URL)
.setClient(client)
.build();
有沒有更好的方法來使用內置改造API(沒有HttpClient擴展)?
從API 9開始,您有java.net.CookieManager
,可以像這樣設置系統范圍的cookie處理程序:
CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(cookieManager);
是的,Apache Http客戶端使用自己的cookie處理機制。 但它應該不是問題,因為從API 9開始HttpURLConnection是推薦的HTTP客戶端。 如果您使用Square的Retrofit,您可能也喜歡他們的OkHttp lib - 具有許多有用功能的自定義URLConnection實現。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.