Http客户端/JSON对象
我正在尝试自动登录发送 JSONObject。我将响应标记为 302 暂时移动,这意味着我应该重定向到另一个网址。但我的response.toString() 显示“位置:/”。下面是代码。
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();
会有什么问题吗?
这是回应。
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=/]
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=/]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您始终可以通过 CURL 甚至浏览器发出请求来手动验证这是否正确。例如,在浏览器地址栏中输入
https://www.hautelook.com/v3/credential
会导致重定向到https://www.hautelook.com
( jQuery 显示了一个漂亮的小登录对话框)。所以你至少知道行为是一致的。这可能意味着以下几种情况之一:
情况)
不太可能,因为在这种情况下我们预计会出现未经授权的 401)
如果不了解有关 API 的更多信息,很难说,但您应该再次查阅文档以确保正确进行调用。
* 编辑*
好的,使用 REST 客户端进行了测试,代码中有一些需要纠正的地方:
原始:
收件人
:这应该允许请求通过,尽管我仍然不确定这是否是正确的端点,因为我返回了一个 HTML 页面。最后一件事,在将代码发布到 SO 之前删除您的 API 凭据总是好的。我在下面添加了请求的屏幕截图:
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 tohttps://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:
case)
unlikely, because we would expect a 401 unauthorized in that case)
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:
Original:
To:
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:
HttpClient 4.x 使用的默认重定向策略遵循 HTTP 规范对实体封装方法(例如 POST 和 PUT)的自动重定向的限制。根据 HTTP 规范的要求,
302 临时移动
、301 永久移动
和307 临时重定向
状态代码可能无法自动处理 POST 和 PUT 方法未经用户明确确认。HttpClient 4.2 可以配置为使用
LaxRedirectStrategy
自动处理所有类型的重定向,而不管规范施加的限制。对于早期版本,可以实现自定义重定向策略,如下所述:Httpclient 4,错误302. 如何重定向?(按照 Bob Kuhar 的建议)。同时我不得不说
'Location: /'
标头看起来有些可疑,甚至自动重定向到该位置也不一定会产生预期的效果。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
and307 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.响应 HTTP/1.1 302 Moved Temporarily 表示发生了一些重定向。
示例案例之一是,需要单点登录(或)授权。如果没有授权 cookie,当您尝试使用 URL 访问资源时,您可能会被重定向到授权,响应为 302。
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.
您只需将 HTTPClient 配置为遵循重定向即可。在 4.x 之前,它只是 HTTPMethod 上的一个方法。添加...
...但这还不够纯粹或其他什么,他们在 4.x 中更改了它。从那以后我就没有尝试过,所以不愿意发布代码。但这个问题之前已经出现过并得到了回答。也许这对你有帮助? Httpclient 4,错误 302。如何重定向?
You could just configure your HTTPClient to follow redirects. Prior to 4.x, it was just a method on the HTTPMethod. Add...
...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?