Java Examples

Java's URL Class

import java.net.*;
import java.io.*;

public class URLConnectionReader {
    public static void main (String[] args) throws Exception {
        URL yahoo = new URL ("http://www.yahoo.com/");
        URLConnection yc = yahoo.openConnection ();
        BufferedReader in = new BufferedReader (
                                new InputStreamReader (
                                yc.getInputStream ()));
        String inputLine;

        while ((inputLine = in.readLine ()) != null) 
            System.out.println (inputLine);
        in.close ();
    }
}


Posting to a URL

try {

    // Construct data
    String data = "hello server!";

    // Send data
    URL url = new URL ("http://hostname:80/servlet");
    URLConnection conn = url.openConnection();
    conn.setDoOutput (true);
    OutputStreamWriter wr = new OutputStreamWriter (conn.getOutputStream ());
    wr.write (data);
    wr.flush ();

    // Get the response
    BufferedReader rd = new BufferedReader (new InputStreamReader (conn.getInputStream ()));
    
    String line;
    while ((line = rd.readLine ()) != null) {
        // Process line...
    }

    wr.close ();
    rd.close ();
}
catch (Exception e) {
}

App Engine REST Server

See attachment below.

ċ
code.zip
(3k)
Ryanne Dolan,
Oct 5, 2010, 8:14 AM
Comments