Sending request from Google App Engine

2019-05-06 19:30发布

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;
}

}

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.

1条回答
老娘就宠你
2楼-- · 2019-05-06 19:52

UPDATE:

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:

Fetching URLs with Google App Engine in Java:

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.

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.

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:

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) {
        // ...
    }
查看更多
登录 后发表回答