用HTTT GET传递cookie

时间:2014-08-02 16:32:59

标签: android cookies http-get reddit

我正在尝试通过reddit api获取用户subreddits列表。目前,我可以为login发布一个返回cookie和modhash的帖子。这些是我传递给我的方法的参数。但是每次我调用该函数时都会得到一个空响应:

  

“{}”

如何通过HTTPGET传递cookie和modhash以获得有效的响应?

public void getUserSubreddits(String[] loginInfo){
    HttpClient client = new DefaultHttpClient();
    URL url = null;
    try {
        url = new URL("http://www.reddit.com/subreddits/mine/.json?limit=100");


        HttpGet httpGet = new HttpGet(String.valueOf(url));
        client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, System.getProperty("http.agent"));
        httpGet.addHeader("cookie", loginInfo[1]);
        httpGet.addHeader("modhash", loginInfo[0]);

        HttpResponse response = client.execute(httpGet);

        HttpEntity ht = response.getEntity();

        BufferedHttpEntity buf = new BufferedHttpEntity(ht);

        InputStream is = buf.getContent();

        BufferedReader r = new BufferedReader(new InputStreamReader(is));

        StringBuilder total = new StringBuilder();
        String line;
        while ((line = r.readLine()) != null) {
            total.append(line);
        }
        Log.d(TAG,total.toString());

    } catch (Exception e) {
        e.printStackTrace();
    }

}

1 个答案:

答案 0 :(得分:0)

这是一个简单的错误。对于将来尝试此操作的任何人,我通过使用Chrome检查http://www.reddit.com/subreddits/mine/.json?limit=100网址上的活动会话的标头来解决问题,并发现Cookie标头以:

开头
  

reddit_session

所以我删除了modhash将my header参数更改为:

  httpGet.addHeader("cookie", "reddit_session="+loginInfo[1]+";");

有了这个,我得到了有效的答复。