我正在开发一个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上,日志打印出“有问题”。经过研究,我尝试了许多实现,但无法让它们运行。 我欣赏任何形式的帮助。
这是伟大的!非常感谢你。你知道从数据中提取响应内容的方法吗?内容是什么是一张图片,我只需要从该方法返回。 – 2012-01-29 10:39:17
同样,这一切都在Google的URLFetch网站的URLFetch文档中。我在上面的答案中增加了一些代码。 – jmort253 2012-01-29 17:37:00