4

I use the following code to get the returned response code of an aspx page

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();

It works fine. But what if the link "http://company.com/temp1.aspx" auto-redirects to another page; assume "http://noncompany.com/temp2.aspx" ? How can i get the response code which is returned from the second link (the one which the first link redirected to) ? Is there something like "follow redirection" to get the new response of the page whom was auto-redirected to ?

Thanks in advance.

Ashraf Bashir
  • 9,070
  • 12
  • 50
  • 79

2 Answers2

8

I found the solution, Here it is for those who are interested:

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;
     }
}
Ashraf Bashir
  • 9,070
  • 12
  • 50
  • 79
  • 1
    You may want to replace the while(true) statement with a for loop (up to 5-10 tries or so) to prevent getting into an infinite redirect loop, which can happen sometimes with an incorrectly configured web server or proxy. – Marc Novakowski Jan 12 '10 at 18:12
  • Thanks Marc for the comment. I think also there's another solution, to keep track of redirected links, by adding each new location (link) to a vector for example, and whenever a new redirection link is found, the code should first check if it exists in the vector, because if it exists there it'll cause cyclic infinite redirection. Thanks for this note. – Ashraf Bashir Jan 13 '10 at 09:42
3

You need to code your HttpConnection inside a loop that follows HTTP redirections based on the response code.

The HTTP header "location" in the response is supposed to give you a new host (maybe it can be used to replace the entire URL).

HttpConnection.HTTP_MOVED_TEMP and HttpConnection.HTTP_MOVED_PERM are the two response code that indicate a redirection.

michael aubert
  • 6,813
  • 1
  • 14
  • 31
  • Thanks Man, I've seen your answer after posting my solution. By the way, there exist another response code which is HttpConnection.HTTP_TEMP_REDIRECT . Thanks again for your reply. I'll mark it as an answer and up. – Ashraf Bashir Jan 12 '10 at 11:44