Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Httpclient / JSONObject

I am trying to autologin sending a JSONObject. Im gettign the reposnse as 302 Moved Temporarily which means I should redirect to another url. But my response.toString() shows "Location: /". Below is the code.

String input_text = "https://www.hautelook.com/v3/credential";
HttpPost httpost = new HttpPost(input_text);
String data =  "{\"screen_resolution\":{\"height\":1080,\"width\":1920}}";

JSONObject jo=new JSONObject();
jo.put("email","sfhgfjk");
jo.put("passsword","dfjhsdkj");
jo.put("meta",data);

StringEntity se = new StringEntity( "JSON: " + json.toString());  
se.setContentEncoding("UTF-8");
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httpost.setEntity(se);

HttpResponse  response = httpclient.execute(httpost);
entity  = response.getEntity();

What would be wrong?

This is the response.

response HTTP/1.1 302 Moved Temporarily [Server: nginx, Content-Type: text/html,
 Location: /, Content-Encoding: gzip, Content-Length: 20, Expires: Thu, 16 Feb 2
012 19:07:55 GMT, Cache-Control: max-age=0, no-cache, no-store, Pragma: no-cache
, Date: Thu, 16 Feb 2012 19:07:55 GMT, Connection: keep-alive, Set-Cookie: PHPSE
SSID=vmoqeksits8ccukvnf7k4rdv75; path=/]
like image 472
Geek Avatar asked Sep 18 '26 18:09

Geek


2 Answers

You can always verify if this is correct manually by issuing the request via CURL, or even your browser. For example, typing https://www.hautelook.com/v3/credential into a browser location bar causes a redirect to https://www.hautelook.com (with a nice little login dialog being shown by jQuery). So you know at least, that the behavior is consistent.

This can mean one of several things:

  • The endpoint you are using is incorrect (this is probably not the case)
  • The authentication information you are supplying is incorrect (also unlikely, because we would expect a 401 unauthorized in that case)
  • The way you are passing the authentication information is incorrect.

Without knowing more about the API its hard to say, but you should consult the docs again to ensure you are making the call correctly.


* EDIT*

Ok, tested with REST client and there are some things to correct in your code:

  • Change 'passsword' to 'password'
  • Change the line:

Original:

new StringEntity( "JSON: " + json.toString())

To:

new StringEntity(json.toString())

This should allow the request through, though I'm still not sure this is the correct endpoint, since I get back an HTML page. One last thing, its always good to remove your API credentials before posting your code to SO. I'm including a screenshot of the request below:

enter image description here

like image 171
Perception Avatar answered Sep 20 '26 06:09

Perception


The default redirect strategy used by HttpClient 4.x honors restrictions on automatic redirection of entity enclosing methods such as POST and PUT imposed by the HTTP specification. Per requirements of the HTTP specification 302 Moved Temporarily, 301 Moved Permanently and 307 Temporary Redirect status codes may not be handled automatically for POST and PUT methods without an explicit confirmation by the user.

HttpClient 4.2 can be configured to use LaxRedirectStrategy that handles all types of redirects automatically regardless of the restrictions imposed by the specification. With earlier versions one can implement a custom redirect strategy as described here: Httpclient 4, error 302. How to redirect? (as suggested by Bob Kuhar).

At the same time I have to say that 'Location: /' header looks somewhat suspicious and even automatic redirect to that location may not necessarily produce the desired effect.

like image 23
ok2c Avatar answered Sep 20 '26 07:09

ok2c