Http GET from GAE Java

2019-09-08 16:17发布

问题:

Well, I really hoped I could handle this without asking here, but I can't :( The idea is pretty simple, you have this link http://cetatenie.just.ro/ and the link is important, because it works for others.

You want to perform a HTTP GET on it from GAE - locally for now, that's it!

If I do it in main class, something like this :

HttpClient httpClient = new DefaultHttpClient();    
HttpGet httpGet = new HttpGet("http://cetatenie.just.ro/");
HttpResponse response = httpClient.execute(httpGet);

And then parse the response everything is fine, now I do the same (almost) thing in GAE - locally (in Eclipse --> Run As --> Web Application), with the help of the Eclipse GAE plugin.

FetchOptions fetchOptions = FetchOptions.Builder.withDeadline(50000);
HTTPRequest request = new HTTPRequest(new URL("http://cetatenie.just.ro/"), HTTPMethod.GET, fetchOptions);
System.out.println("ULR-->" + request.getURL());    
URLFetchService service = URLFetchServiceFactory.getURLFetchService();
HTTPResponse response = service.fetch(request);
System.out.println("RESPONSE_CODE-->" + response.getResponseCode());
String responseAsString = new String(response.getContent());
System.out.println("RESPONSE_AS_STRING-->" + responseAsString);

I always get a Http 500 error code. I know that this indicates on a server problem, but how come it works from my stand-alone application?

Is GAE doing something fishy?

Regards, Eugene.

回答1:

Damn! This was easy actually - when sending the request I was missing two headers (seems like GAE is like a telnet - not sending any headers unless you specify them). Anyhow here is what I added:

 URL url = new URL("http://cetatenie.just.ro");
 URLConnection urlConnection = url.openConnection();
 urlConnection.setRequestProperty("Host", "cetatenie.just.ro");
 urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; Linux i686; rv:7.0.1) Gecko/20100101 Firefox/7.0.1");