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