簡體   English   中英

Apache HttpClient:無法檢索從Android設備中的服務器發送的cookie

[英]Apache HttpClient: Cannot retrieve cookies sent from the server in Android device

在Android應用程序中使用Apache HttpClient時遇到一個奇怪的問題。

我的應用程序需要登錄到網站並獲取一些數據,然后注銷。

我當前的代碼如下所示:

public class BlueClient {
    private String hostUrl;
    private DefaultHttpClient client;
    private HttpContext localContext;


    public BlueClient(String hostUrl,DefaultHttpClient httpClient,HttpContext localContext) {

        this.hostUrl = hostUrl;
        this.client = httpClient;
        this.localContext = localContext;

    }

    public boolean doLogin(String userName,String password){
        String url = getHostUrl()+"do_login";//loggin will set a session cookie
        HttpPost post = new HttpPost(url);

        try {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("username",userName));
            nameValuePairs.add(new BasicNameValuePair("password",password));
            post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = client.execute(post,localContext);

            //ok now if response is ok then return true

        } catch (Exception e) {

        }
        return false;
    }

    public MyStuff getMyStuff(){
        String url = getHostUrl()+"/getMyStuff/"; //this url requires authentication. the sesion cookie should do that 
        HttpGet get = new HttpGet(url);
        try {
            HttpResponse response = client.execute(get,localContext);
            //ok get my stuff from response and return
        } catch (Exception e) {

        }
        return null;
    }
    public boolean doLogout(){
    String url = getHostUrl()+"do_logout";//it clears the cookie so the session is invalidated
    HttpGet get = new HttpGet(url);
    try {
        HttpResponse response = client.execute(get,localContext);
        //ok the cookie is cleared
      }
    } catch (Exception e) {

    }
    return false;
  }
}

當我調用這些函數時,我確實喜歡這樣。 它可以在模擬器中運行,但不能在設備中運行

HttpContext context = new BasicHttpContext();
CookieStore cookieStore = new BasicCookieStore();
context.setAttribute(ClientContext.COOKIE_STORE,cookieStore);
DefaultHttpClient httpClient = new DefaultHttpClient();

BlueClient myClient = new BlueClient("http://myHost.com/",httpClient,context);

myClient.doLogin("user","pass");
 // it should've printed the cookies set by the server but i get nothing here !
D.log(context.getAttribute(ClientContext.COOKIE_STORE));
// as this is another subsequesnt request it shoud carry the cookies back to the server but as the cookies are not set this function gives me nothig :-(
myClient.getMyStuff();
myClient.doLogout();

任何人都可以對此有所了解。 為什么它在設備中不起作用?

我終於找到問題了!

我的服務器在CodeIgniter上運行。 然后CodeIgniter將會話cookie(ci_session)的到期日期設置為符合Netscape cookie草稿。 格式為EEE,dd-MMM-yy HH:mm:ss zzz ,HttpClient使用的默認CookiePoicy為RFC_2109。 因此,當httpClient嘗試解析cookie數據時,它將在解析到期日期時失敗。 我必須明確設置Cookie政策和日期格式。

所以我的最終代碼如下所示:

BasicHttpParams params = new BasicHttpParams();
String[] dateFormats = {"EEE, dd-MMM-yy HH:mm:ss zzz"};
params.setParameter(CookieSpecPNames.DATE_PATTERNS,Arrays.asList(dateFormats));

DefaultHttpClient httpClient = new DefaultHttpClient(params);
httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY,CookiePolicy.NETSCAPE);//explicitly set the cookie policy

HttpContext context = new BasicHttpContext();
CookieStore cookieStore = new BasicCookieStore();
context.setAttribute(ClientContext.COOKIE_STORE,cookieStore);


BlueClient myClient = new BlueClient("http://myHost.com/",httpClient,context);

myClient.doLogin("user","pass");
myClient.getMyStuff();//now i get my stuff !
myClient.doLogout();

希望它可以節省別人的時間:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM