3

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=/]
Geek
  • 3,187
  • 15
  • 70
  • 115

4 Answers4

2

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

Perception
  • 79,279
  • 19
  • 185
  • 195
1

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.

Community
  • 1
  • 1
ok2c
  • 26,450
  • 5
  • 63
  • 71
  • any other options or techniques to autologin? – Geek Feb 17 '12 at 15:23
  • @javaiText: there is no such thing as autologin in HTTP. HTTP agents must follow the protocol and comply with expectations of the HTTP server. – ok2c Feb 17 '12 at 16:39
0

response HTTP/1.1 302 Moved Temporarily means, some redirection happened.

One of the example case is, Single sign on (or) Authorization required. Without authorization cookie when you try to access the resource using URL you may be redirected for Authorization with response as 302.

kosa
  • 65,990
  • 13
  • 130
  • 167
0

You could just configure your HTTPClient to follow redirects. Prior to 4.x, it was just a method on the HTTPMethod. Add...

HttpPost httpost = new HttpPost(input_text);
httpost.setFollowRedirect( true );

...but this wasn't pure enough or something and they changed it in 4.x. I haven't tried it since, so am reluctant to post code. But this question has come up and been answered here before. Maybe this helps you? Httpclient 4, error 302. How to redirect?

Community
  • 1
  • 1
Bob Kuhar
  • 10,838
  • 11
  • 62
  • 115
  • i dont see setFollowRedirect method in HttpClient. – Geek Feb 16 '12 at 19:54
  • oh...my bad...its on the HttpMethod (httppost in your case). I'll edit my answer. – Bob Kuhar Feb 16 '12 at 19:57
  • Not in post either. let me check the API. – Geek Feb 16 '12 at 20:03
  • It was there in older HTTPClients. At 4.x, they moved it. I posted a link above. Good luck. – Bob Kuhar Feb 16 '12 at 20:05
  • @ Bob Kuhar: 'Follow redirects' flag is on by default for all versions of HttpClient since 2.x. httpost.setFollowRedirect( true ) basically makes no difference. As to your commend about API changes, HttpClient 3.x was an utter mess where 95% of protocol handling logic was crammed into one class making it a nightmare to maintain. The reason for HttpClient 3.x end of life was simple: no one, just no one, was willing to maintain it. – ok2c Feb 17 '12 at 11:54