HttpResponse代码302

时间:2012-04-26 08:30:34

标签: java http blackberry http-status-code-302

我正在使用模拟器BB 8900.我正在尝试连接到url并获取响应代码302.这是什么意思?  这是我的代码片段:

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
.....
connection = (HttpConnection)Connector.open(url);
responseCode = connection.getResponseCode();

1 个答案:

答案 0 :(得分:8)

An HTTP 302 is a 'temporary 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/

您需要从响应中提取新URL。 (使用getHeaderField("Location")执行此操作)。然后在您获得的新URL上执行相同的方法。

另外两点:

  1. 由于这是“临时”重定向,因此您无法存储此新网址。您应该继续使用旧的,如果它返回302,则使用“位置”中的任何URL。

  2. 如果您没有执行GET或HEAD,则不应自动执行重定向。而是要求用户干预。 The RFC requires this