Android Http获取Session Cookie

Blu*_*ell 9 cookies android http httpclient http-headers

我真的不想在这里发帖,因为网上有太多的信息,但是我已经深入了解并无法弄明白.

好的,所以我不能让这个在两个场景中工作,希望两者的答案是一样的.

我的问题是我设置了请求标头,但它似乎没有发送它.

我有一个会话ID s = e32ff223fwefd3,我想将它存储在"Cookie"下,但它似乎没有工作.

这是我有最快的代码示例

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.HttpResponse;

private static String sessionCookie = "s=12342342352354234";

public static void get(String url) {
    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet(url);
        if(sessionCookie != null){
            Log.d(TAG, "Setting Cookie: "+sessionCookie);
            request.setHeader("Cookie", sessionCookie);
        } else {
            Log.i(TAG, "Null session request get()");
        }
        HttpResponse response = client.execute(request);

        Header[] headers = response.getAllHeaders();
        for (int i=0; i < headers.length; i++) {
            Header h = headers[i];
            Log.i(TAG, "Header names: "+h.getName());
            Log.i(TAG, "Header Value: "+h.getValue());
        }
}
Run Code Online (Sandbox Code Playgroud)

所以,当我的反应出来时,另一方它没有附加我的s = 232342w3f23f id!

我想我已经解释了这一点,任何帮助都表示赞赏

Blu*_*ell 5

好吧上面的代码确实有效,我只是没有正确检索标题以查看所有字段.问题是我有一个无效的会话ID但我使用的网络服务器有WORST错误处理机制:-)感谢收听!