CS6320:  SW Engineering of Web Based Systems

 

Google App Engine: URL Fetch

 

  • your app can use URL Fectch to connect to remote hosts using HTTP

  • uses scalable service called the URL Fetch service.

  • supports both http (port 80 only) and https (port 443 only)

  • supports only (for scalability): GET, POST, PUT, HEAD, and DELETE

  • uses HTTP 1.1 protocol

  • can NOT connect to urls owned by webapp itself (prevents looping)

  • URL Fetch service waits up to five seconds (see documentation for current value) for a response from the remote server. If the server does not respond by the deadline, the service throws an IOException. You can adjust the amount of time to wait using API. (java =e setConnectTimeout() method of the URLConnection)

 

SAME AS Standard Java (well, replaces java.net.URLConnection with a GAE service-based implementation) --- but, for programmer is the same

         import java.net.URL;
import java.net.MalformedURLException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.BufferedReader; // ... try { URL url = new URL("http://ae-book.appspot.com/blog/atom.xml/"); InputStream inStream = url.openStream(); InputStreamReader inStreamReader = new InputStreamReader(inStream); BufferedReader reader = new BufferedReader(inStreamReader); // ... read characters or lines with reader ... reader.close(); } catch (MalformedURLException e) { // ... } catch (IOException e) { // ... }

 

URL Fetch Limits

URL FETCH: can issue requests up to 1 megabyte, and receive responses up to 32 megabytes.

 

 

GAE URL Fectch in Java setting parameters -- use low level GAE api classes

 

   import java.net.URL;
   import java.net.MalformedURLException;
   import com.google.appengine.api.urlfetch.FetchOptions;
   import com.google.appengine.api.urlfetch.HTTPMethod;
   import com.google.appengine.api.urlfetch.HTTPRequest;
   import com.google.appengine.api.urlfetch.HTTPResponse;
   import com.google.appengine.api.urlfetch.ResponseTooLargeException;
   import com.google.appengine.api.urlfetch.URLFetchService;
   import com.google.appengine.api.urlfetch.URLFetchServiceFactory;
   // ...
   try {
       URL url = new URL("http://ae-book.appspot.com/blog/atom.xml/");
       FetchOptions options = FetchOptions.Builder.doNotFollowRedirects().disallowTruncate();
       HTTPRequest request = new HTTPRequest(url, HTTPMethod.GET, options);
       URLFetchService service = URLFetchServiceFactory.getURLFetchService();
       HTTPResponse response = service.fetch(request);
       // ... process response.content ...
   } catch (ResponseTooLargeException e) {
       // ...
   } catch (MalformedURLException e) {
       // ...
   } catch (IOException e) {
       // ...
   }
© Lynne Grewe