Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java http connection

Tags:

java

http

https

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????

like image 478
user323101 Avatar asked Sep 01 '10 14:09

user323101


People also ask

What is HTTP URL connection in Java?

HttpURLConnection class is an abstract class directly extending from URLConnection class. It includes all the functionality of its parent class with additional HTTP-specific features. HttpsURLConnection is another class that is used for the more secured HTTPS protocol.

When should I use HttpURLConnection?

The method is used to enable streaming of a HTTP request body without internal buffering, when the content length is not known in advance. It sets whether HTTP redirects (requests with response code) should be automatically followed by HttpURLConnection class.

Can HttpURLConnection be used with https?

HttpsURLConnection extends HttpURLConnection , and your connection is an instance of both. When you call openConnection() the function actually returns an HttpsURLConnection . However, because the https object extends the http one, your connection is still an instance of an HttpURLConnection .


1 Answers

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(); }
    }
like image 82
Mike G Avatar answered Oct 03 '22 14:10

Mike G