0
我有沒有爲我在兩個活動使用證書檢查的DefaultHttpClient:使用DefaultHttpClient活動之間
public function clientWithoutCertificateCheck() {
DefaultHttpClient httpClient = new DefaultHttpClient();
try{
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("https", sf, 443));
ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
httpClient = new DefaultHttpClient(ccm, params);
} catch (Exception e) { }
return httpClient;
}
而且它在這同樣的活動內頁登錄。
啓動後,其他活動我保存它的cookie:
CookieSyncManager.createInstance(this);
List<Cookie> cookies = httpClient.getCookieStore().getCookies();
for(Cookie cookie : cookies)
{
String cookieString = cookie.getName() + "=" + cookie.getValue() + "; domain=" + cookie.getDomain();
CookieManager.getInstance().setCookie(cookie.getDomain(), cookieString);
}
然後我試圖得到相同的HttpClient在另一個活動。
DefaultHttpClient httpClient = clientWithoutCertificateCheck();
String url = "https://academicos.unilasalle.edu.br/";
String[] keyValueSets = CookieManager.getInstance().getCookie(url).split(";");
for(String cookie : keyValueSets)
{
String[] keyValue = cookie.split("=");
String key = keyValue[0];
String value = "";
if(keyValue.length>1) value = keyValue[1];
httpClient.getCookieStore().addCookie(new BasicClientCookie(key, value));
}
由於某種原因,他沒有再登錄。
有人可以幫我解決這個問題嗎?
如何從其他活動獲取原始httpClient?發佈您的clientWithoutCertificateCheck()方法可能會幫助其他人找到您的問題。 – yorkw
我改進了這個問題。爲了獲得一個新的Http客戶端,第一部分代碼在這兩個活動中都得到了重複。 –