Submit a request from Google App Engine

I am developing a play web application that needs to be deployed to a Google application. I am trying to send a request to a different server than to process the response. It works fine on my localhost but I am having difficulty when I test it on GAE. The code is as follows:

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 do not receive an error message with e.printStackTrace () ;. In GAE, the magazine prints "a problem has occurred." I tried many implementations after research, but could not get them to work. I appreciate any help.

+4
source share
1 answer

UPDATE:

Before you abandon the current Fetch library, make sure that the server is public. Please note that the App Engine development server uses the network configuration of your computer when executing queries; thus, if the URL you are trying to retrieve is accessible to your network but not offline, this can cause problems.

If you confirmed that the URL is indeed public, read:

Getting URLs using Google App Engine in Java :

The Google App Engine has a very clear set of requirements for creating HTTP requests from App Engine. Although other methods may work from your local development server; often these same methodologies do not work in production.

Check out the URLFetch documentation. It describes at least two different ways to use the low-level URLFetch service or java.net library for an HTTP request.

The following is an example of using java.net, which I believe is very 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) { // ... } 
+9
source

Source: https://habr.com/ru/post/1393570/


All Articles