【问题标题】:Sending request from Google App Engine从 Google App Engine 发送请求
【发布时间】:2012-01-29 03:09:52
【问题描述】:

我正在开发一个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 上,日志打印出“出现问题”。经过研究,我尝试了许多实现,但无法让它们中的任何一个运行。 感谢您的任何帮助。

【问题讨论】:

    标签: java google-app-engine playframework


    【解决方案1】:

    更新:

    在放弃当前的 URL Fetch 库之前,请确保服务器可以公开访问。请注意,App Engine 开发服务器在发出请求时会使用您计算机的网络配置;因此,如果您尝试从中获取的 URL 可以访问您的网络但不能在网络外部访问,那么这可能会导致问题。

    如果您已验证该 URL 确实可以公开访问,请继续阅读:

    在 Java 中使用 Google App Engine 获取 URL:

    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) {
            // ...
        }
    

    【讨论】:

    • 这太棒了!非常感谢你。您知道从该数据中提取响应内容的任何方法吗?内容中的内容是图片,我只需要从方法返回。
    • 同样,这一切都在 Google 的 URLFetch 网站上的 URLFetch 文档中。我在上面的答案中添加了更多代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-02
    • 2014-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-18
    • 2019-03-05
    相关资源
    最近更新 更多