从Google App Engine发送请求
问题描述:
我正在开发一个play Web应用程序,该应用程序应该部署在Google应用程序引擎上。我正在尝试向另一台服务器发送请求,而不是处理响应。在我的本地主机它是完美运行然而,当我在GAE上测试时遇到困难。代码如下:从Google App Engine发送请求
import com.google.appengine.repackaged.org.apache.http.HttpResponse;
import com.google.appengine.repackaged.org.apache.http.client.methods.HttpGet;
import com.google.appengine.repackaged.org.apache.http.conn.scheme.PlainSocketFactory;
import com.google.appengine.repackaged.org.apache.http.conn.scheme.Scheme;
import com.google.appengine.repackaged.org.apache.http.conn.scheme.SchemeRegistry;
import com.google.appengine.repackaged.org.apache.http.impl.client.DefaultHttpClient;
import com.google.appengine.repackaged.org.apache.http.impl.conn.SingleClientConnManager;
import com.google.appengine.repackaged.org.apache.http.params.BasicHttpParams;
public class Getter{
public static byte[] getStuff(){
String urlString = "http://example.com/item?param=xy";
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
BasicHttpParams params = new BasicHttpParams();
SingleClientConnManager connmgr = new SingleClientConnManager(params, schemeRegistry);
DefaultHttpClient client = new DefaultHttpClient(connmgr, params);
HttpGet get = new HttpGet(urlString);
byte[] buf = null;
try {
HttpResponse resp = client.execute(get);
buf = new byte[(int) resp.getEntity().getContentLength()];
resp.getEntity().getContent().read(buf);
} catch (Exception e) {
System.out.println("There was a problem.");
e.printStackTrace();
}
return buf;
}
}
可悲的是,我没有得到任何错误消息e.printStackTrace();在GAE上,日志打印出“有问题”。经过研究,我尝试了许多实现,但无法让它们运行。 我欣赏任何形式的帮助。
答
UPDATE:
之前放弃当前的网址提取库,确保该服务器是公共访问。请注意,App Engine开发服务器在提出请求时使用您的计算机的网络配置;因此,如果您试图从中获取的URL可以访问您的网络,但不在网络外部,则可能会导致问题。
如果你验证过的网址确实是公开访问,那么请往下看:
与谷歌应用程序引擎抓取的URL在Java中:
谷歌应用程序引擎有一个非常清晰的从App Engine发出HTTP请求的要求。虽然其他方法可能从您的本地开发服务器上运行;通常,这些相同的方法不适用于生产。
查看URLFetch文档。它概述了至少两种使用低级别URLFetch服务或java.net库进行HTTP请求的不同方法。
下面是一个例子使用java.net,我已经发现是高度可靠:
import java.net.MalformedURLException;
import java.net.URL;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
// ...
try {
URL url = new URL("http://www.example.com/atom.xml");
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
while ((line = reader.readLine()) != null) {
// ...
}
reader.close();
} catch (MalformedURLException e) {
// ...
} catch (IOException e) {
// ...
}
HTTP POST:
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
// ...
String message = URLEncoder.encode("my message", "UTF-8");
try {
URL url = new URL("http://www.example.com/comment");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write("message=" + message);
writer.close();
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
// OK
} else {
// Server returned HTTP error code.
}
} catch (MalformedURLException e) {
// ...
} catch (IOException e) {
// ...
}
这是伟大的!非常感谢你。你知道从数据中提取响应内容的方法吗?内容是什么是一张图片,我只需要从该方法返回。 – 2012-01-29 10:39:17
同样,这一切都在Google的URLFetch网站的URLFetch文档中。我在上面的答案中增加了一些代码。 – jmort253 2012-01-29 17:37:00