从Google App Engine发送请求 [英] Sending request from Google App Engine

查看:101
本文介绍了从Google App Engine发送请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发 play 网络应用程序,该应用程序应该部署在Google应用程序引擎上。我正在尝试向另一台服务器发送请求,而不是处理响应。在我的本地主机完美地工作,但是我在GAE上测试时遇到困难。代码如下:

I am developing a play web application which is supposed to be deployed on Google app engine. I am trying to send a request to another server than process the respond. On my localhost it is working perfectly however I have difficulties when I test it on GAE. The code is the following:

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上,日志打印出有问题。经过研究,我尝试了许多实现,但无法让它们运行。
我很感激任何帮助。

The sad thing is that I don't get any error message with e.printStackTrace();. On GAE the log prints out "There was a problem". I tried many implementation after researched but couldn't get any of them running. I appreciate any kind of help.

推荐答案

更新:

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

Before abandoning your current URL Fetch library, make sure that the server is publicly accessible. Note that the App Engine development server uses the network configuration of your computer when making requests; thus, if the URL you're trying to fetch from is accessible to your network but not outside the network, then this could cause problems.

如果您已验证该网址确实可公开访问,请阅读:

If you've verified that the URL is indeed publicly accessible, then please read on:

使用Java中的Google App Engine获取网址

Google App Engine对于从App Engine发出HTTP请求有一系列非常明确的要求。虽然其他方法可能从您的本地开发服务器上运行;通常情况下,这些相同的方法不适用于制作。

Google App Engine has a very clear set of requirements for making HTTP requests from App Engine. While other methods may work from your local development server; oftentimes, those same methodologies don't work in production.

查看 URLFetch 文档。它概述了至少两种使用低级URLFetch服务或java.net库进行HTTP请求的不同方法。

Check out the URLFetch documentation. It outlines at least two different ways of using either the low level URLFetch service or the java.net library to make an HTTP request.

下面是使用java.net的一个示例,我发现它非常可靠:

Below is an example using java.net, which I've found to be highly reliable:

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:

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 App Engine发送请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆