本文介绍了Android的HTTP GET会话Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我并不是真的想张贴在这里,因为是在网络上这么多的信息,但我已经拖网深处,不能看着办吧。

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.

我有一个会话ID S = e32ff223fwefd3,我想这个存储在曲奇,但它似乎并不奏效。

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

下面是最快的code比如我有

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

所以,当我反应出来的另一边就没有我的S = 232342w3f23f ID连接到它!

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

我想我已经解释了这一权利,任何帮助是pciated AP $ P $

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

推荐答案

OK了code以上实际上并工作,我只是没有检索标题正确地看到所有的字段。这个问题是我有一个无效的会话ID,但我用的是web服务器有最坏的错误处理:-)感谢机制听!

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!

这篇关于Android的HTTP GET会话Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 05:50