When you use the HttpConnection class in MIDP to access a web page in a given URL, it might be the case that the URL points to a file that is actually located elsewhere. In these cases the HTTP response you get is not the page you were trying to access but a link to the new file.
The MIDP API does not follow page redirects automatically, its up to you to implement that.
I have written a small class that helps doing this.
To illustrate the use of this class see the following program (this code is only the run method of a class that is Runnable):
public void run() {
Thread thisThread= Thread.currentThread();
HttpConnection c = null;
InputStream is = null;
// we only want to have one instance of this thread running
if (thisThread != currentThread) {
return;
}
try {
System.out.println(thisThread+ ":" + url);
c = (HttpConnection)Connector.open(url);
/* read HTTP Headers */
String field, key;
StringBuffer texto = new StringBuffer();
int i = 0;
do {
field = c.getHeaderField(i);
key = c.getHeaderFieldKey(i);
/* if we get a 'location' header then this is a redirect */
if (key != null && key.equals("location")) {
/* build the absolute URL of the target file */
url = HTTPRedirect.redirectURL(url, field);
/* let's launch another thread to read the new URL */
currentThread = new Thread(this);
currentThread.start();
return;
}
i++;
} while (key != null);
/* read the HTTP response body, we now have the correct file */
[...]
} catch (IOException ioe) {
} finally {
try {
if (is != null) {
is.close();
}
if (c != null) {
c.close();
}
} catch (IOException ioe) {}
}
}
The redirectURL() method is implemented in the HTTPRedirect class and is used to construct the absolute URL of the target file given the URL of the current file and the location header content:
package org.jorgecardoso.net;
/**
* Utility class for HTTP redirects.
*
* Contains static methods that build the absolute URL
* based on the original URL (the one that originated the
* redirect response) and on the location
* header.
*
*
* @author Jorge Cardoso
* @version 1.00, 16/07/2005
*/
public class HTTPRedirect {
/**
* Returns the URL of the redirected webpage.
*
* <code>baseURL</code> is the requested HTTP URL and <code>
* location</code> is the HTTP Location Header value.
*
* @param baseURL The requested HTTP URL.
* @param location The Location Header value.
*
* @return An absolute URL that corresponds to the target URL.
*/
public static String redirectURL(String baseURL, String location) {
if (location.startsWith("http://")) {
return location;
} else if (location.startsWith("/")) {
return "http://" + getHost(baseURL) + "location";
} else {
if (baseURL.endsWith("/")) {
return baseURL + location;
} else {
return baseURL + "/" + location;
}
}
}
/**
* Returns the host part of the URL, without the trailing slash.
* http://jorgecardoso.org/ - returns jorgecardoso.org
* @param URL The URL to get the host part from. Must start with http://
*
* @return The host part of the URL.
*/
public static String getHost(String URL) {
if (!URL.startsWith("http://")) {
throw new java.lang.IllegalArgumentException("URL must start with 'http://'");
}
String n = URL.substring(7); // strip http://
int slashPos = n.indexOf('/');
if ( slashPos == -1) { // no slash, so this is the host only
return n;
} else {
return n.substring(0, slashPos+1);
}
}
/**
* Indicates whether this URL has a file part.
*
* @param URL The URL to check for the file part.
*
* @return true if this URL has a file part, false otherwise.
*/
public static boolean hasFile(String URL) {
URL = URL.trim();
if (!URL.startsWith("http://")) {
throw new java.lang.IllegalArgumentException("URL must start with 'http://'");
}
String n = URL.substring(7); // strip http://
int lastSlashPos = n.lastIndexOf('/');
if (lastSlashPos == -1) { //no slash, so no file
return false;
} else if (lastSlashPos < n.length()) {
return true;
}
return false;
}
}
You can download this file here: HTTPRedirect