2010-08-09 30 views
0

我可以得到一個cookie與Firefox,但不是與Android。如何使用android獲取缺少的cookie?在Firefox是出現

這是代碼:

HttpParams httpparams = new BasicHttpParams();       
httpparams.setParameter(ClientPNames.COOKIE_POLICY,CookiePolicy.BROWSER_COMPATIBILITY); 

HttpGet httpget = new HttpGet(sURL); 
httpget.setParams(httpparams); 
httpget.setHeader("User-Agent","Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2"); 
httpget.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); 
httpget.setHeader("Accept-Language", "es-mx,es;q=0.8,en-us;q=0.5,en;q=0.3"); 
httpget.setHeader("Accept-Encoding", "gzip,deflate"); 
httpget.setHeader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7"); 
httpget.setHeader("Keep-Alive", "115"); 

BasicHttpResponse response = (BasicHttpResponse) httpclient.execute(httpget); 

使用wireshrack我看到:

GET /login.php HTTP/1.1 
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
Accept-Language: es-mx,es;q=0.8,en-us;q=0.5,en;q=0.3 
Accept-Encoding: gzip,deflate 
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 
Keep-Alive: 115 
Host: www.conquerclub.com 
Connection: Keep-Alive 

和響應在使用我的代碼:

HTTP/1.1 200 OK 
Date: Mon, 09 Aug 2010 05:41:38 GMT 
Server: Apache/2.0.52 (Red Hat) 
X-Powered-By: PHP/5.2.13 
Set-Cookie: referer=%5Bdirect%5D; path=/ 
Set-Cookie: referer60=%5Bdirect%5D; expires=Wed, 08-Sep-2010 05:41:38 GMT; path=/ 
Connection: close 
Transfer-Encoding: chunked 
Content-Type: text/html 

但蒙山Firefox的反應是

HTTP/1.1 200 OK 
Date: Mon, 09 Aug 2010 05:11:13 GMT 
Server: Apache/2.0.52 (Red Hat) 
X-Powered-By: PHP/5.2.13 
Set-Cookie: referer=%5Bdirect%5D; path=/ 
Set-Cookie: referer60=%5Bdirect%5D; expires=Wed, 08-Sep-2010 05:11:13 GMT; path=/ 
Set-Cookie: PHPSESSID=sv8f6ro571t9rv999mu6jtkbu3; path=/; HttpOnly 
Expires: Thu, 19 Nov 1981 08:52:00 GMT 
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 
Pragma: no-cache 
Content-Encoding: gzip 
Vary: Accept-Encoding 
Content-Length: 4324 
Connection: close 
Content-Type: text/html 

再次使用Wireshark來看看Firefox的我得到這個:

GET/HTTP/1.1 
Host: www.conquerclub.com 
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
Accept-Language: es-mx,es;q=0.8,en-us;q=0.5,en;q=0.3 
Accept-Encoding: gzip,deflate 
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 
Keep-Alive: 115 
Connection: keep-alive 

這是相同的,但服務器不響應此行

Set-Cookie: PHPSESSID=sv8f6ro571t9rv999mu6jtkbu3; path=/; HttpOnly 

爲什麼呢?任何想法都會得到很好的接受。

+0

看看這裏:http://stackoverflow.com/questions/678630/how-do-i-make-an-http-request-using-cookies-在Android和這裏:http://stackoverflow.com/questions/1652850/android-webview-cookie-problem,HTH – 2010-08-09 06:11:40

+0

謝謝喬治,是的,我看到它。我之前使用過一頁來獲取代碼的另一部分代碼。 – user383343 2010-08-09 06:28:55

回答

0

我遇到了類似的問題與android。發生這種情況是因爲瀏覽器兼容模式中的cookie來源策略。 Android中的HTTP客戶端拒絕Cookie,因爲cookie的路徑與當前請求文檔的路徑不匹配。

例如如果您的Cookie路徑爲:/mypath,並且您的Cookie源自:/mypath/myAdditionalPath,則http客戶端將拒絕Cookie(內部拋出MalformedCookieException)。但瀏覽器會接受這樣的cookie。

要在代碼中解決此問題,您必須實現自己的Cookie規範,該規範從BrowserCompatSpec開始。下面是一些示例框架代碼:

public class MyCookieSpec extends BrowserCompatSpec { 

     @Override 
     public void validate(Cookie cookie, CookieOrigin origin) throws 
      MalformedCookieException { 
     if(cookie == null) { 
      throw new IllegalArgumentException("Cookie cannot be null"); 
     } 
     if(origin == null) { 
      throw new IllegalArgumentException("Cookie origin cannot be null"); 
     } 


     // use these logs to see what is the difference between paths of the 
     // cookieOrigin and the cookie. 
     String pth = cookie.getPath(); 
     Log.i(TAG, "Cookie ====================================> " + cookie); 
     Log.i(TAG, "CookieOrigin ====================================> " + origin); 

     // Check if the cookie is from the same domain, if so return silently 
     // or else throw a MalformedCookieException 

     } 

     @Override 
     public boolean match(Cookie cookie, CookieOrigin origin) { 
     if(cookie == null) { 
      throw new IllegalArgumentException("Cookie cannot be null"); 
     } 
     if(origin == null) { 
      throw new IllegalArgumentException("Cookie origin cannot be null"); 
     } 
     if(Log.isLoggable(TAG, Log.DEBUG)) { 
      Log.d(TAG, "Matching cookie " + cookie + " with origin " + origin); 
     } 

     // if the cookie is originating from the same domain as of the origin 
     // return true or return false. Be careful here and only return true if 
     // the cookie is originating from the same domain as that of what is in the 
     // cookie's path 
     return true; 
     } 
    } 

要註冊並使用這個cookie規範與HTTP客戶端,使用以下命令:

DefaultHttpClient httpClient = new DefaultHttpClient(cm, params); 
    httpClient.getCookieSpecs().register("myspec", new CookieSpecFactory() { 

    public CookieSpec newInstance(HttpParams hp) { 
     return new MyCookieSpec(); 
    } 
    }); 
    httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, "myspec"); 
+0

感謝naikus,我用你的代碼和日誌顯示只有兩個餅乾(referer,referer60),但沒有一個(PHPSESSID) 我在想這個問題是服務器 – user383343 2010-08-11 01:38:26

0

對不起球員。我意識到,我犯了一個錯誤 與Firefox,我要求www.conquerclub.com與路徑「/」 但使用我的代碼要求www.conquerclub.com與路徑「/login.php」

我刪除後它「login.php」,請求返回3 cookies