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.
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With