java HttpResponse 代码 302
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10329779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HttpResponse code 302
提问by JoVinz
I am using simulator BB 8900. I am trying to connect to url and get response code 302.What does it mean? Here is my code snippet:
我正在使用模拟器 BB 8900。我正在尝试连接到 url 并获得响应代码 302。这是什么意思?这是我的代码片段:
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
.....
connection = (HttpConnection)Connector.open(url);
responseCode = connection.getResponseCode();
回答by ArjunShankar
An HTTP 302 is a 'temporary redirect'. You need to handle it.
HTTP 302 是“临时重定向”。你需要处理它。
As per the standard, if you get a 302 response, the response will contain a 'Location' header field with the redirect:
根据标准,如果您收到 302 响应,则响应将包含一个带有重定向的“位置”标头字段:
Client request:
GET /index.html HTTP/1.1
Host: www.example.com
Server response:
HTTP/1.1 302 Found
Location: http://www.redirected-address.example.com/
You need to extract the new URL from the response. (Use getHeaderField("Location")
to do this). Then execute the same method on the new URL you got.
您需要从响应中提取新 URL。(getHeaderField("Location")
用于执行此操作)。然后在你得到的新 URL 上执行相同的方法。
Two other points:
另外两点:
Since this is a 'temporary' redirect, you cannot store this new URL. You should keep using the old one, and if it returns a 302, then use whatever URL is in 'Location'.
If you are not executing a GET or HEAD, you shouldn't do the redirect automatically. Instead ask for user intervention. The RFC requires this.
由于这是“临时”重定向,因此您无法存储此新 URL。您应该继续使用旧的,如果它返回 302,则使用“位置”中的任何 URL。
如果您没有执行 GET 或 HEAD,则不应自动执行重定向。而是要求用户干预。RFC 要求这样做。