我正在开发一个基于play框架的web应用程序,打算部署在Google app engine上。我试图向另一个服务器发送请求并处理响应。在我的本地主机上,它完美地工作,但是当我在GAE上测试时遇到了困难。代码如下:
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上,日志会打印出“有问题发生”。我经过研究尝试了很多实现方式,但都无法正常运行。
非常感谢任何形式的帮助。