0

I am trying to get a list of user subreddits via the reddit api. Currently I am able to do a post for login which returns a cookie and modhash. Those are the parameters I'm passing to my method below. However each time I call the function I get an empty response:

"{}"

How can I pass a cookie and modhash via HTTPGET to get a valid response?

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

}
Community
  • 1
  • 1
Nick
  • 9,285
  • 33
  • 104
  • 147

1 Answers1

0

It was a simple mistake. To anyone in the future trying this I solved the problem by using Chrome to inspect the headers of an active session on http://www.reddit.com/subreddits/mine/.json?limit=100 url and found that the cookie header started with:

reddit_session

So I removed the modhash changed the my header parameter to read:

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

With this I got a valid response.

Nick
  • 9,285
  • 33
  • 104
  • 147