Http get with Java 

Joined:
04/09/2007
Posts:
753

August 21, 2008 22:35:24    Last update: October 16, 2008 02:27:59
import java.net.*;
import java.io.*;

public class HttpGet {
    public static void main(String[] args) throws Exception {
        BufferedReader input;

        URL url = new URL("http://localhost:12803/demo/");
        URLConnection urlConn = url.openConnection();
        urlConn.setDoInput(true);
        urlConn.setDoOutput(false);
        urlConn.setUseCaches(false);

        urlConn.setRequestProperty("Cookie", "JSESSIONID=c0a815013203b3eabca1a54140299b01d5d655fa903d;secondCookie=cookie value");

        // the following line is not necessary for GET, left here as example in case POST is needed 
        urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

        // Get response data.
        input = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
        String str;
        while (null != ((str = input.readLine()))) {
            System.out.println(str);
        }
        input.close();
    }
}


An example for POSTing from Java is available from here: http://www.javaworld.com/javaworld/javatips/jw-javatip34.html
Share |
| Comment  | Tags