如何按照"页面自动重定向"来获取响应代码?

Ash*_*hir 4 blackberry java-me rim-4.5 blackberry-jde blackberry-os-v4.5

我使用以下代码获取aspx页面的返回响应代码

HttpConnection connection 
     = (HttpConnection) Connector.open("http://company.com/temp1.aspx" 
                                       + ";deviceside=true");
connection.setRequestMethod(HttpConnection.GET);
connection.setRequestProperty(HttpHeaders.HEADER_CONNECTION, "close");
connection.setRequestProperty(HttpHeaders.HEADER_CONTENT_LENGTH, "0");
int resCode = connection.getResponseCode();
Run Code Online (Sandbox Code Playgroud)

它工作正常.但是,如果链接" http://company.com/temp1.aspx "自动重定向到另一个页面,该怎么办?假设" http://noncompany.com/temp2.​​aspx "?如何获取从第二个链接(第一个链接重定向到的链接)返回的响应代码?是否有类似"跟随重定向"的东西来获取自动重定向到的页面的新响应?

提前致谢.

Ash*_*hir 8

我找到了解决方案,对于那些感兴趣的人:

int resCode;
String location = "http://company.com/temp1.aspx";
while (true) {  
     HttpConnection connection = (HttpConnection) Connector.open(location + ";deviceside=true");
     connection.setRequestMethod(HttpConnection.GET);
     connection.setRequestProperty(HttpHeaders.HEADER_CONNECTION, "close");
     connection.setRequestProperty(HttpHeaders.HEADER_CONTENT_LENGTH, "0");
     resCode = connection.getResponseCode();
     if( resCode == HttpConnection.HTTP_TEMP_REDIRECT
          || resCode == HttpConnection.HTTP_MOVED_TEMP
          || resCode == HttpConnection.HTTP_MOVED_PERM ) {
          location = connection.getHeaderField("location").trim();
     } else {
          resCode = connection.getResponseCode();
          break;
     }
}
Run Code Online (Sandbox Code Playgroud)