Java ME应用设计指南之联网重定向
2009-02-05 08:46
211 查看
http 1.1协议允许web服务器临时改变资源的位置,也就是说你访问的资源在另外一个地址。这时候服务器返回的响应代码是302,而新的地址存放在header中,header的名称是location。正常情况下,客户端浏览器应该指向临时的访问地址。但是,移动终端设备差距很大,在处理302返回码的时候,设备之间的行为差异很大。
下面的代码可以用来处理重定向问题,但是在部分设备中会导致应用程序出错。
connection c = (httpconnection) connector.open(uri);
int status = c.getresponsecode();
string new_uri = c.getheaderfield("location"); // new_uri is null on some devices
if (status == 302) {
c.close();
c = (httpconnection) connector.open(new_uri); // breaks here
}
由于重定向是http 1.1的特性,那么所有1.1兼容的设备都需要考虑这个问题。下面介绍如何解决这个问题。
事实证明在某些设备上,底层的网络协议栈处理重定向的问题,302响应码告诉应用程序内部的处理流程。应用程序应该等待直到响应码等于302。但是有些设备不能正确地从响应中解析出location字段,这样location字段的内容是null,响应码存储在了响应的内容之中。有经验的工程师会采用下面的解决办法。
1)解析响应,在location或者响应的内容中查找新地址,如果找到的话关闭以前的连接,转向新的连接。
2)如果什么也没有找到的话,那么等待10-1000ms,直到状态码从302转变为200。马上处理响应,当作没有错误发生。
下面的代码能够很好的解决重定向的问题,供大家参考和完善。
connection c = (httpconnection) connector.open(uri);
int status = c.getresponsecode();
string redirection = httpconnection.getheaderfield("location");
if (status == httpconnection.http_temp_redirect) {
if (redirection != null) {
// this the standard http 1.1 behaviour, move on to the redirection uri (basically restarting again).
} else {
// parse the content of the http response, if any.
// lookup for a "location" header, if found, set value to the redirection variable
if (redirection != null) {
// since location was found, fall back to the standard behaviour.
} else {
long begin_wait = system.currenttimemillis();
while (system.currenttimemillis() - begin_wait < 1000 || response != 200) {
sleep(100);
response = httpconnection.getresponsecode();
};
if (response == 200) {
// once again we're back on tracks, continue processing as if no error has ever happen
} else {
// here we're really hopeless. either the server did provided a valid redirection uri,
// or the device did not preserved it. the best option is probably to fail by throwing an exception.
};
};
};
} else // handle other error codes here
};
// handle success here (status == 200)
您还可以了解一下http协议的细节,http://www.ietf.org/rfc/rfc2616.txt。本文是笔者在阅读sun的技术文章的时候编译的。您可以通过下面的地址阅读原文,也欢迎您编译其他的好文章,共同促进国内java me技术的发展。
下面的代码可以用来处理重定向问题,但是在部分设备中会导致应用程序出错。
connection c = (httpconnection) connector.open(uri);
int status = c.getresponsecode();
string new_uri = c.getheaderfield("location"); // new_uri is null on some devices
if (status == 302) {
c.close();
c = (httpconnection) connector.open(new_uri); // breaks here
}
由于重定向是http 1.1的特性,那么所有1.1兼容的设备都需要考虑这个问题。下面介绍如何解决这个问题。
事实证明在某些设备上,底层的网络协议栈处理重定向的问题,302响应码告诉应用程序内部的处理流程。应用程序应该等待直到响应码等于302。但是有些设备不能正确地从响应中解析出location字段,这样location字段的内容是null,响应码存储在了响应的内容之中。有经验的工程师会采用下面的解决办法。
1)解析响应,在location或者响应的内容中查找新地址,如果找到的话关闭以前的连接,转向新的连接。
2)如果什么也没有找到的话,那么等待10-1000ms,直到状态码从302转变为200。马上处理响应,当作没有错误发生。
下面的代码能够很好的解决重定向的问题,供大家参考和完善。
connection c = (httpconnection) connector.open(uri);
int status = c.getresponsecode();
string redirection = httpconnection.getheaderfield("location");
if (status == httpconnection.http_temp_redirect) {
if (redirection != null) {
// this the standard http 1.1 behaviour, move on to the redirection uri (basically restarting again).
} else {
// parse the content of the http response, if any.
// lookup for a "location" header, if found, set value to the redirection variable
if (redirection != null) {
// since location was found, fall back to the standard behaviour.
} else {
long begin_wait = system.currenttimemillis();
while (system.currenttimemillis() - begin_wait < 1000 || response != 200) {
sleep(100);
response = httpconnection.getresponsecode();
};
if (response == 200) {
// once again we're back on tracks, continue processing as if no error has ever happen
} else {
// here we're really hopeless. either the server did provided a valid redirection uri,
// or the device did not preserved it. the best option is probably to fail by throwing an exception.
};
};
};
} else // handle other error codes here
};
// handle success here (status == 200)
您还可以了解一下http协议的细节,http://www.ietf.org/rfc/rfc2616.txt。本文是笔者在阅读sun的技术文章的时候编译的。您可以通过下面的地址阅读原文,也欢迎您编译其他的好文章,共同促进国内java me技术的发展。
相关文章推荐
- Java ME应用设计指南之联网重定向
- Java ME应用设计指南之联网重定向
- DotNET应用架构设计指南(第二章:设计应用程序和服务组件(6-9)) 已发布
- 6-读书笔记----iOS开发指南:从零基础到App Store上架--iOS-iPhone与iPad应用开发的差异和iOS分层架构设计
- Android应用的Material设计中图片的相关处理指南
- 苹果分享 ARKit 应用人机界面设计指南
- 手机应用标签栏的设计指南
- 【转】MEF程序设计指南三:MEF中组合部件(Composable Parts)与契约(Contracts)的基本应用
- Web开发设计:DisplayTag应用指南
- DotNET应用架构设计指南(第一章:介绍(2-2))上传
- DotNET应用架构设计指南(第二章:设计应用程序和服务组件(1-4))已上传
- HAproxy指南之haproxy重定向应用(案例篇)
- Web开发设计:DisplayTag应用指南
- DotNET应用架构设计指南(第三章:安全 运行管理和通讯策略(13-16))
- MEF程序设计指南三:MEF中组合部件(Composable Parts)与契约(Contracts)的基本应用
- Web开发设计:DisplayTag应用指南
- Windows 8 Metro应用设计指南中文版
- PL/SQL DEVELOPER 6 模板格式设计应用指南(原译)
- 工业接口RS-485的设计应用指南