我尝试使用Reddit API(https://github.com/reddit/reddit - www.reddit.com/dev)。
这是我的HttpClientHelper
:
public HttpClientHelper(String path)
{
this.url = "www.reddit.com";
this.path = path;
if(httpClient == null)
{
httpClient = new DefaultHttpClient();
}
}
public void addParamForGet(String key, String value)
{
dataGet.add(new BasicNameValuePair(key, value));
}
public void addParamForPost(String key, String value)
{
dataPost.add(new BasicNameValuePair(key, value));
}
public HttpResponse executePost()
{
HttpResponse response = null;
try {
uri = URIUtils.createURI(METHOD, url, PORT, path,
dataGet == null ? null : URLEncodedUtils.format(dataGet, "UTF-8"), null);
HttpPost httpPost = new HttpPost(uri);
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
httpPost.setEntity(new UrlEncodedFormEntity(dataPost, HTTP.UTF_8));
response = httpClient.execute(httpPost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (URISyntaxException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return response;
}
public HttpResponse executeGet()
{
try {
uri = URIUtils.createURI(METHOD, url, PORT, path,
dataGet == null ? null : URLEncodedUtils.format(dataGet, "UTF-8"), null);
} catch (URISyntaxException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
HttpGet httpget = new HttpGet(uri);
HttpResponse response = null;
try {
response = httpClient.execute(httpget);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return response;
}
当我尝试登录Reddit时,它可以工作:
HttpClientHelper client = new HttpClientHelper(Endpoints.User.login);
client.addParamForPost("api_type", "json");
client.addParamForPost("user", username);
client.addParamForPost("passwd", password);
client.addParamForPost("rem", String.valueOf(true));
client.executePost();
结果:{"json":{"data":{"cookie":"30310021,2014-10-27T16:02:27,300937022ed465f695747a2aa7fd**********","need_https":false,"modhash":"9ch7btilr85ce7c2a427ef87bdb422132f738c3a1**********"},"errors":[]}}
以下是返回的Cookie(黯然失色):
List<Cookie> cookies = client.getHttpClient().getCookieStore().getCookies();
for (int i = 0; i < cookies.size(); i++) {
Log.d("", cookies.get(i).toString());
}
[version: 0][name: __cfduid][value: d****7d2bfa753ef32b2f2861c117b8c141444******][domain: .reddit.com][path: /][expiry: Mon Dec 23 23:50:00 GMT 2019]
[version: 0][name: reddit_session][value: 30310021%2C2014-10-27T15%3A18%3A39%2C5**08054f8fa07d82aa0ac027a2f**********][domain: reddit.com][path: /][expiry: Thu Dec 31 23:59:57 GMT 2037]
在以下检索用户数据/api/me.json
的请求中,它失败:
HttpClientHelper client = new HttpClientHelper(Endpoints.User.me);
client.executeGet();
结果:{}
但是,当我打印cookie时,结果是相同的(这是预期的,因为我使用相同的DefaultHttpClient实例):
List<Cookie> cookies = client.getHttpClient().getCookieStore().getCookies();
for (int i = 0; i < cookies.size(); i++) {
Log.d("", cookies.get(i).toString());
}
[version: 0][name: __cfduid][value: d****7d2bfa753ef32b2f2861c117b8c141444******][domain: .reddit.com][path: /][expiry: Mon Dec 23 23:50:00 GMT 2019]
[version: 0][name: reddit_session][value: 30310021%2C2014-10-27T15%3A18%3A39%2C5**08054f8fa07d82aa0ac027a2f**********][domain: reddit.com][path: /][expiry: Thu Dec 31 23:59:57 GMT 2037]
我做错了什么?
感谢。
答案 0 :(得分:0)
旧帖但......
您没有在检索用户数据时指出失败的性质。
我观察到的一件事是reddit_session cookie的域名为“reddit.com”,而另一个域名为“.reddit.com”。当您使用'reddit.com而不是'www.reddit.com'时,前者只会被发送回服务器,而后者将被发送到所有子域。也许这就是失败的原因。
如果您实际上使用“www.reddit.com”域名作为原始请求,我很困惑您已经收回了这些cookie。