4
如何在DefaultHttpClient
和WebView
之間共享cookie?如何在DefaultHttpClient和WebView之間共享Cookie
如何在DefaultHttpClient
和WebView
之間共享cookie?如何在DefaultHttpClient和WebView之間共享Cookie
用戶解決了在DefaultHttpClient和WebView之間共享Cookie的問題。 This solution worked for him,所以他想在那裏分享完整的代碼。
LoginActivity.java,核心代號爲doPost方法:
private int mNumber = 3;
public InputStream doPost(String url, HashMap<String, String> params,
String headParam, ArrayList<String> keyValues) {
DefaultHttpClient httpClient = null;
InputStream inputStream = null;
HttpResponse httpResponse = null;
int statusCode = -1;
httpClient = (DefaultHttpClient) NetworkManager.getHttpClient();
HttpPost httpPost = new HttpPost(url);
if (headParam != null) {
httpPost.addHeader("Cookie", headParam);
}
if (params != null) {
List<NameValuePair> httpRequestParams = new ArrayList<NameValuePair>();
Iterator<Entry<String, String>> iter = params.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<String, String> entry = iter.next();
String key = entry.getKey();
String val = entry.getValue();
if (val.equals("multi")) {
for (String values : keyValues)
httpRequestParams.add(new BasicNameValuePair(key,
values));
} else
httpRequestParams.add(new BasicNameValuePair(key, val));
}
try {
httpPost.setEntity(new UrlEncodedFormEntity(httpRequestParams,
HTTP.UTF_8));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
try {
httpResponse = httpClient.execute(httpPost);
} catch (ClientProtocolException e) {
e.printStackTrace();
httpPost.abort();
} catch (IOException e) {
e.printStackTrace();
httpPost.abort();
if (mNumber >= 1) {
mNumber--;
doPost(url, params, headParam, keyValues);
return null;
}
}
if (httpResponse != null) {
statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == HttpURLConnection.HTTP_OK) {
try {
inputStream = httpResponse.getEntity().getContent();
} catch (IllegalStateException e) {
e.printStackTrace();
httpPost.abort();
} catch (IOException e) {
e.printStackTrace();
}
} else {
if (mNumber >= 1) {
mNumber--;
doPost(url, params, headParam, keyValues);
} else {
}
}
} else {
if (mNumber >= 1) {
mNumber--;
doPost(url, params, headParam, keyValues);
}
}
Config.mCookies = httpClient.getCookieStore().getCookies(); //save cookies
return inputStream;
}
在Config.java:
public static List<Cookie> mCookies = null;
成功登錄後,網頁視圖瀏覽:
List<Cookie> cookies = Config.mCookies;
if (cookies != null && !cookies.isEmpty()) {
CookieSyncManager.createInstance(mContext);
CookieManager cookieManager = CookieManager.getInstance();
for (Cookie cookie : cookies) {
Cookie sessionInfo = cookie;
String cookieString = sessionInfo.getName() + "="
+ sessionInfo.getValue() + "; domain="
+ sessionInfo.getDomain();
cookieManager.setCookie("http://stackoverflow.com", cookieString);
CookieSyncManager.getInstance().sync();
}
}
mWebview.loadUrl(mLink);
setCookie(url, string);
的網址需要主機,起初他只使用域(例如,stackoverflow.com
),但它不起作用。它也必須包含主機(例如,http://stackoverflow.com
)。
感謝分享解決方案,但我建議編輯這個帖子,以遵守SO的規則 - >讓你的問題聽起來更像是問題(即「如何分享cookie ......」),然後回答你的問題自己的問題)。最後,如果沒有人提供更好的答案,請接受你自己的答案。 –
也許你可以編輯它,並創建一個正確的答案。我想你會以這種方式獲得更多的選票。 –
我不明白你如何鏈接web視圖與餅乾。它如何知道它必須加載在webView的請求中的cookie –