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

本文介绍在Nokia S60设备上使用HttpConnection遇到302 HTTP状态时的处理方法。当遇到重定向时,直接读取输入流会导致程序崩溃。文章提供了针对Nokia N90等特定型号手机的解决方案,并给出了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

历史

Version

Date

Creator

Description

1.0.0.1

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



Trackback: http://tb.blog.youkuaiyun.com/TrackBack.aspx?PostId=971232


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值