I’m running a java code for getting info from a google-app-engine web service (testeserjaum.appspot.com), but it always prints {null=[HTTP/1.1 302 Found], Date=[Wed, 26 Oct 2011 21:02:46 GMT], Content-Length=[0], Location=[https://testeserjaum.appspot.com/], Content-Type=[text/html], Server=[Google Frontend]} on the first System.out.println and null on the second one.
The funny fact is that, with other google-app-engine web services, the code works perfectly (i tried with testegelfur.appspot.com and it worked).
Here’s the code:
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class URLConnectionReader {
public static void main(String[] args) throws MalformedURLException, IOException{
URL url = new URL("http://testeserjaum.appspot.com/");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
System.out.println(urlConnection.getHeaderFields());
try {
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader buff = new BufferedReader(new InputStreamReader(in));
System.out.println(buff.readLine());
}finally{
urlConnection.disconnect();
}
}
}
And, I don’t know if it helps, but here’s a python equivalent that actually works. I can’t use it because I plan to use this software on an android application, and I have a large amount of the software done, so this have to be done in java.
import urllib2
import base64
req = urllib2.Request('http://testeserjaum.appspot.com/')
req.add_header('User-Agent', 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)')
#base64string = base64.encodestring('%s:%s' %('user', 'pass'))
#print base64string
#req.add_header("Authorization", "Basic %s" %base64string)
lol = urllib2.urlopen(req)
print lol.read()
HTTP 302 is a redirect, not an error. That’s why it’s called Found.
Specifically, it tells the client to fetch
https://testeserjaum.appspot.com/instead ofhttp://testeserjaum.appspot.com/.If you don’t want to deal with the redirect, just access
https://testeserjaum.appspot.com/directly.