从Google App Engine发送请求

时间:2012-01-29 03:09:52

标签: java google-app-engine playframework

我正在开发一个play网络应用程序,该应用程序应该部署在Google应用引擎上。我正在尝试向另一台服务器发送请求,而不是处理响应。在我的 localhost 上,正常工作但是当我在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上,日志打印出“有问题”。我在研究后尝试了很多实现,但无法运行它们。 我感谢任何帮助。

1 个答案:

答案 0 :(得分:9)

<强>更新

在放弃当前的网址抓取库之前,请确保该服务器可公开访问。请注意,App Engine开发服务器在发出请求时使用计算机的网络配置;因此,如果您尝试提取的URL可供您的网络访问,但不能访问网络外,则可能会导致问题。

如果您确认该网址确实可公开访问,请继续阅读:

使用Google App Engine获取Java

Google App Engine对从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) {
        // ...
    }