android cookies issue in application

2019-06-12 15:54发布

hy i am developing an application that has to connect to a webserver and download and read json format data. i am able to login in it, but whenever i try to fetch other data, it returns failure.

i tried the same thing on browser by copy-pasting the urls using get method and it works. the application uses cookies to store the data.

how can i store cookies in an android application & send them to server?

标签: android http
2条回答
女痞
2楼-- · 2019-06-12 16:28

Just check android development reference: http://developer.android.com/reference/java/net/HttpURLConnection.html

There is "Sessions with Cookies" paragraph ;-)

查看更多
\"骚年 ilove
3楼-- · 2019-06-12 16:38

In your connecting thread, set the cookie in your request header like this (sCookie is my static cookie String variable):

HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
//.....
if(sCookie!=null && sCookie.length()>0){
    conn.setRequestProperty("Cookie", sCookie);
}

Below that, after receiving a response, get the cookie like this:

String cookie = conn.getHeaderField("set-cookie");
if(cookie!=null && cookie.length()>0){
    sCookie = cookie;
}
查看更多
登录 后发表回答