Android Http 获取会话 Cookie

发布于 2024-09-26 05:29:06 字数 1167 浏览 0 评论 0原文

我本来不想在这里发帖,因为网上有太多信息,但我已经深入搜寻但无法弄清楚。

好吧,所以我无法让它在两种情况下工作,希望这两种情况的答案是相同的。

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

我有一个会话 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());
        }
}

所以当我的回复从另一边出来时它没有附加我的 s=232342w3f23f id!

我想我已经解释得对了,感谢任何帮助

I didn't really want to post here as there is so much information on the net, but I've trawled the depths and can't figure it out.

Ok so I can't get this to work in two scenario's hopefully the answer is the same for both.

My problem is I set the request header but it doesn't seem to send it.

I have a session id s=e32ff223fwefd3 , and I want to store this under "Cookie" , but it doesn't seem to be working.

Here is the quickest code example I have

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());
        }
}

So when my response comes out the other side it doesn't have my s=232342w3f23f id attached to it!

I think I've explained this right, any help is appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ゃ懵逼小萝莉 2024-10-03 05:29:06

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

Ok the code above actually does work, I just didn't retrieve the header correctly to see all the fields. The issue was I had an invalid session Id but the webserver I was using has the WORST error handling mechanisms :-) thanks for listening!

十级心震 2024-10-03 05:29:06

在发送请求之前,在您的 DefaultHttpClient 对象上尝试 getCookieStore().addCookie()

Try getCookieStore().addCookie() on your DefaultHttpClient object, before sending the request.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文