来自GAE Java的HTTP GET [英] Http GET from GAE Java

查看:152
本文介绍了来自GAE Java的HTTP GET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我真的希望我可以在不询问这里的情况下处理这个问题,但我不能:(
)这个想法非常简单,你有这个链接 http://cetatenie.just.ro/ ,该链接很重要,因为它适用于其他人。



你想在GAE上执行一个HTTP GET - 本地现在,就是这样!



如果我在主类中执行它,如下所示:



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

然后解析响应一切都很好,现在我在GAE - 本地(在Eclipse - > Run As - > Web Application)中执行相同的(几乎)操作,并借助于Eclipse GAE插件。

  FetchOptions fetchOptions = FetchOptions.Builder.withDeadline(50000); 
HTTPRequest request = new HTTPRequest(new U RL(http://cetatenie.just.ro/),HTTPMethod.GET,fetchOptions);
System.out.println(ULR - >+ request.getURL());
URLFetchService服务= 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);

我总是得到一个Http 500错误代码。我知道这表示服务器存在问题,但是它是如何从我的独立应用程序中运行的?



GAE做了些什么吗?



问候,
尤金。

解决方案

这实际上很简单 - 发送请求时,我缺少两个头(看起来像GAE就像一个telnet - 除非指定它们,否则不会发送任何头文件)。无论如何,这是我补充说:

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


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.

解决方案

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

这篇关于来自GAE Java的HTTP GET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆