I'm trying to use the Reddit API (- www.reddit.com/dev).
Here is my HTTP ClientHelper
:
public HTTP ClientHelper (string path) {this.url = "www.reddit.com"; this. Path = path; If (httpClient == faucet) {httpClient = New DefaultHttpClient (); }} Public Zero addParamForGet (string key, string value) {dataGet.add (New BasicNameValuePair (key, value)); } Public Zero addParamForPost (string key, string value) {dataPost.add (New BasicNameValuePair (key, value)); } Public HttpResponse executePost () {HttpResponse response = zero; {URI = URIUtils.createURI (Method, URL, Port, Path, Datagate == Empty: Empty: urlcodeedUutili format (Dataset, "UTF-8", blank); Http post http post = new HTTP post (Yuri); HttpPost.setHeader ("content-type", "app / x-www-form-urxed"); HttpPost.setEntity (new UrlEncodedFormEntity (Datapost, HTTP.UTF_8)); Feedback = httpClient.execute (httpPost); } Catch (Client Protocol E) {// Todo Auto-Generated Catch Block e.printStackTrace (); } Grip (IOException E) {// TODO Auto-Generated Catch Block e.printStackTrace (); } Grip (URIXtexExtension E1) {// TODO Auto-Generated Cache Block e1.printStackTrace (); } Return response; } Public HttpResponse executeGet () {try {URI = URIUtils.createURI (METHOD, url, PORT, path, dataGet == is empty? Empty: URL encoded high format (dataset, "UTF-8", blank); } Grip (URIXtexExtension E1) {// TODO Auto-Generated Cache Block e1.printStackTrace (); } HttpGet httpget = new HttpGet (Yuri); HttpResponse response = null; Try {response = httpClient.execute (httpget); } Catch (Client Protocol E) {// Todo Auto-Generated Catch Block e.printStackTrace (); } Grip (IOException E) {// TODO Auto-Generated Catch Block e.printStackTrace (); } Return response; }
When I try to login at reddit, it works:
http clienthalper client = new HTTP clientHelper (endpoint.user Login); Client.addParamForPost ("API_prop", "Jason"); Client.addParamForPost ("user", user name); Client.addParamForPost ("passwd", password); Client.addParamForPost ("RIM", String.valueOf (true)); Client.executePost (); Result: {"json": {"data": {"cookie": "30310021,2014-10-27T16: 02: 27,300937022AD 465f695747a2aa7fd *** ** ***** "," need_https ": false," modeshash ":" 9ch7btilr85ce7c2a427ef87bdb422132f738c3a1 ********** "}," Errors ": []}}
Cookies return here (heavy):
list & lt; Cookie & gt; Cookies = client.getHttpClient (). GetCookieStore () GetCookies (); (Int i = 0; i & lt; cookies.size (); i ++) {log d. ("", Cookies.get (i) .toString ()); } [Name: __cfduid] [value: d **** 7d2bfa753ef32b2f2861c117b8c141444 ******] [domain: .reddit.com] [path: /] [end: Mon 23 23:50: 00 GMT 2019] [version : 0] [Name: reddit_session] [Price: 30310021% 2C2014-10-27T15% 3A18% 3A339% 2C5 ** 08054f8fa07d82aa0ac027a2f **********] [Domain: reddit .com] [Path: /] [Closing: Thursday 31 December 23:59:57 GMT 2037]
On receiving the following request for receiving user data, / api / me .json
, this fails:
http clienthalper client = new HTTP clientHelper (endpoint.user.im); Client.executeGet ();
Result: {}
But when I print cookies, the result is the same (it is expected, As I have used the same example of DefaultHttpClient):
list & lt; Cookie & gt; Cookies = client.getHttpClient (). GetCookieStore () GetCookies (); (Int i = 0; i & lt; cookies.size (); i ++) {log d. ("", Cookies.get (i) .toString ()); } [Name: __cfduid] [value: d **** 7d2bfa753ef32b2f2861c117b8c141444 ******] [domain: .reddit.com] [path: /] [end: Mon 23 23:50: 00 GMT 2019] [version : 0] [Name: reddit_session] [Price: 30310021% 2C2014-10-27T15% 3A18% 3A339% 2C5 ** 08054f8fa07d82aa0ac027a2f **********] [Domain: reddit .com
What am I doing wrong?
Thank you.
Older posts but ...
Failure to retrieve you Unidentified user data
One thing I see is that reddit_session cookie has a domain of 'reddit.com', whereas the second domain is '.reddit.com' when you are protesting against 'www.reddit.com' If you use reddit.com, the former will only be sent to the server, while the next will be sent to all sub-domains. Perhaps this was the reason for the failure.
I am surprised that you have taken those cookies back, although if you were actually using the domain 'www.reddit.com' for the original request.
Comments
Post a Comment