16

I want setup a http connection to send request and get the response in an stand alone java application, can any one help me how can i proceed with this????

user323101
  • 259
  • 1
  • 8
  • 13
  • 2
    By the way, feel free to "accept" an answer if it has been most helpful to you. (though you might be an unregistered user, and I'm not sure if that's possible?) – Mike G Sep 03 '10 at 13:20

3 Answers3

16
HttpURLConnection connection = null;
    try {
        URL url = new URL("www.google.com");
        connection = (HttpURLConnection) url.openConnection();
        connection.connect();
        connection.getInputStream();
                    // do something with the input stream here

    } catch (MalformedURLException e1) {
        e1.printStackTrace();
    } catch (IOException e1) {
        e1.printStackTrace();
    } finally {
        if(null != connection) { connection.disconnect(); }
    }
Mike G
  • 4,713
  • 2
  • 28
  • 31
2

You can use URLConnection class bundled with standard Java (since JDK 1.0!), or a higher level HTTP client such as Apache's HTTPCLIENT which will provide, in addition to plain HTTP, higher level components like cookies, standard headers and more.

Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
0

A couple of answers have already pointed out Apache HTTP Client, but they link to version 3.x, which is no longer maintained. You should use version 4, which has a slightly different API, if you want to use this library: http://hc.apache.org/httpcomponents-client-4.0.1/index.html

Bruno
  • 119,590
  • 31
  • 270
  • 376