问题描述
我有在Android的HttpClient的一个问题:通过使用下面的code,我想用这已经设置之前,通过一个web视图登录的cookie。因此,登录数据应该在那里,确实有,我测试了它。但是,当我用饼干在httppost或HTTPGET它不使用的登录数据。但这些饼干其实应该足以接收网页需要登录是必要的,应该不是吗?我真的不知道,如果我需要发送的cookie以特殊的方式给服务器左右,或者如果它足以加载到HttpContext的。这里是code:
I have a problem with the HttpClient in Android: By using the following code, I want to use the cookies which are already set before by logging in through a webview. So the login data should be there and is indeed there, I tested it. But when I use the cookies in an httppost or httpget it doesn't use the login data. but these cookies actually should be enough to receive that page for which a login is necessary, shouldn't they? I'm not really sure if I need to send the cookies in a special way to the server or so or if it is enough to load it into the httpcontext. Here is the code:
DefaultHttpClient httpclient = new DefaultHttpClient();
CookieStore lCS = new BasicCookieStore();
if (CookieManager.getInstance().getCookie(pUrl) != null) {
String cookieString = CookieManager.getInstance().getCookie(pUrl);
String[] urlCookieArray = cookieString.split(";");
for (int i = 0; i < urlCookieArray.length; i++) {
System.out.println(urlCookieArray[i]);
String[] singleCookie = urlCookieArray[i].split("=");
Cookie urlCookie = new BasicClientCookie(singleCookie[0], singleCookie[1]);
lCS.addCookie(urlCookie);
}
}
HttpContext localContext = new BasicHttpContext();
httpclient.setCookieStore(lCS);
localContext.setAttribute(ClientContext.COOKIE_STORE, lCS);
HttpPost httppost = new HttpPost(pUrl);
// get the url connection
try {
StringBuilder sb = new StringBuilder();
HttpResponse response = httpclient.execute(httppost, localContext);
InputStream is = response.getEntity().getContent();
InputStreamReader isr = new InputStreamReader(is);
如果我运行code我只收到该网站的登录页面,因此不接受该cookie。
And if I run the code I only receive the login page of that site, so it didn't accept the cookie.
感谢您的帮助提前
映入眼帘,蒂莫
推荐答案
我有同样的问题,我用类似的方法,因为在没有运气的问题。这使得它为我工作的事情是添加域为每个复制的cookie。(BasicClientCookie cookie.setDomain(字符串))
I had the same problem and I used similar approach as in the question with no luck.The thing that made it work for me was to add the domain for each copied cookie.(BasicClientCookie cookie.setDomain(String))
我的UTIL功能:
public static BasicCookieStore getCookieStore(String cookies, String domain) {
String[] cookieValues = cookies.split(";");
BasicCookieStore cs = new BasicCookieStore();
BasicClientCookie cookie;
for (int i = 0; i < cookieValues.length; i++) {
String[] split = cookieValues[i].split("=");
if (split.length == 2)
cookie = new BasicClientCookie(split[0], split[1]);
else
cookie = new BasicClientCookie(split[0], null);
cookie.setDomain(domain);
cs.addCookie(cookie);
}
return cs;
}
String cookies = CookieManager.getInstance().getCookie(url);
BasicCookieStore lCS = getCookieStore(cookies, MyApp.sDomain);
HttpContext localContext = new BasicHttpContext();
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.setCookieStore(lCS);
localContext.setAttribute(ClientContext.COOKIE_STORE, lCS);
...
这篇关于Android的HttpClient的和Cookies的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!