在检查用户名或密码是否正确时,我从服务器获取 HttpResponse
。
当我在 webview
中加载 url
时,我希望 webView
有 cookie(我通过 postData()
存储在 webView
中。
我希望 webView 获取 cookie 并使用存储在 webview 中的 cookie 加载 url。
我正在收到回复。
public HttpResponse postData() {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://example.com/login.aspx");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("txtUsername", "user"));
nameValuePairs.add(new BasicNameValuePair("txtPassword", "123"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
String responseAsText = EntityUtils.toString(response.getEntity());
Log.v(TAG , "Response from req: " + responseAsText);
return responseAsText;
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
return null;
}
我用以下方式加载Url:
webView.loadUrl("http://a_page.com/getpage.aspx?p=home");
我想我并没有真正管理 cookie,而且我不知道该怎么做。 有什么建议或解决方案吗?
最佳答案
其实很简单。
String cookieString = "cookie_name=cookie_value; path=/";
CookieManager.getInstance().setCookie(baseUrl, cookieString);
其中 cookieString
的格式与更传统的 Set-Cookie
HTTP header 相同,而 baseUrl
是 cookie 应属于的站点.
关于android - 在 Android 中将 cookie 设置为 webView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5716898/