Http cookie store in Android

前端 未结 1 1767
抹茶落季
抹茶落季 2020-12-13 07:39

I am developing an Android client for the site with authorization. I have a post method. Example my code:

public void run() {
    handler.sendMessage(Message         


        
相关标签:
1条回答
  • 2020-12-13 08:33

    You get your cookies from HttpResponse response:

    Header[] mCookies = response.getHeaders("cookie");
    

    and add them to your next request:

    HttpClient httpClient = new DefaultHttpClient();
    
    //parse name/value from mCookies[0]. If you have more than one cookie, a for cycle is needed.
    CookieStore cookieStore = new BasicCookieStore();
    Cookie cookie = new BasicClientCookie("name", "value");
    cookieStore.addCookie(cookie);
    
    HttpContext localContext = new BasicHttpContext();
    localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
    
    HttpGet httpGet = new HttpGet("http://www.domain.com/"); 
    
    HttpResponse response = httpClient.execute(httpGet, localContext);
    
    0 讨论(0)
提交回复
热议问题