Nokia S60如何处理302 HTTP状态[J2ME]

[j2me] Nokia S60如何处理302 HTTP状态

历史

Version

Date

Creator

Description

1.0.0.2

2006-7-24

郑昀

第一稿

 

1 Nokia S60如何处理302HTTP状态

 

HttpConnection/302/ HTTP_TEMP_REDIRECT

关键词

详细描述

当用HttpConnection读取远端数据,而远端返回状态码302表示重定向时,继续调用openInputStream来读取输入流将会导致程序崩溃。

 

此种现象发生在以下机型:

Nokia N90/

6600/6630/6680

 

N70不会崩溃但也不会正常运行。

根据协议规定,此时的Location头域中保存了你应该重新请求的地址。

请看

http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.3 来了解更多关于"302 Found"

 

也就是说,此时用HttpConnection.getHeaderField("Location")来得到具体的跳转url,然后重新向新地址发起请求。

 

代码示范:

 

private HttpConnection open(String url) throws IOException {

HttpConnection c;

int status = -1;

 

// Open the connection and check for redirects

while (true) {

c = (HttpConnection) Connector.open(url);

 

// Get the status code,

// causing the connection to be made

status = c.getResponseCode();

 

if ((status == HttpConnection.HTTP_TEMP_REDIRECT)

|| (status == HttpConnection.HTTP_MOVED_TEMP)

|| (status == HttpConnection.HTTP_MOVED_PERM)) {

 

// Get the new location and close the connection

url = c.getHeaderField("location");

c.close();

} else {

break;

}

}

 

// Only HTTP_OK (200) means the content is returned.

if (status != HttpConnection.HTTP_OK) {

c.close();

throw new IOException("Response status not OK");

}

return c;

}

 

 

参考资料1RFC2616

10.3.3 302 Found

The requested resource resides temporarily under a different URI. Since the redirection might be altered on occasion, the client SHOULD continue to use the Request-URI for future requests. This response is only cacheable if indicated by a Cache-Control or Expires header field.

The temporary URI SHOULD be given by the Location field in the response. Unless the request method was HEAD, the entity of the response SHOULD contain a short hypertext note with a hyperlink to the new URI(s).

If the 302 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.

      Note: RFC 1945 and RFC 2068 specify that the client is not allowed
      to change the method on the redirected request.  However, most
      existing user agent implementations treat 302 as if it were a 303
      response, performing a GET on the Location field-value regardless
      of the original request method. The status codes 303 and 307 have
      been added for servers that wish to make unambiguously clear which
      kind of reaction is expected of the client.

 

参考资料2Nokia Forum

KVM crashes when reading content with HTTP "302 Found" response on N90 and 6680

posted @ 2006-07-24 22:57  老兵笔记  阅读(3961)  评论(5编辑  收藏  举报