1

I am working on HttpURLConnection class to do some network activity from my application. So i am wondering how the HttpURLConnection works internally. I gone through the class and found the connect() method defined as an abstract in URLConnection.

But i couldn't able to find the connect() method definition. So i am quite curious that , how HttpURLConnection connect to the server.

MyCode:

        URL url = new URL("https://example.com");
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestProperty("Accept-Encoding", "identity");
        connection.setRequestProperty("Authorization", basicAuth);
        connection.setRequestProperty("Connection", "keep-alive");
        connection.setRequestProperty("Content-type", "");
        connection.setRequestProperty("User-Agent", "LibHttp/1.3.8");
        connection.setRequestMethod("POST");

        connection.connect(); // how it connects //

Please suggest me some solution.

Anshuman Pattnaik
  • 883
  • 3
  • 16
  • 37
  • Maybe this could help you [Java URLConnection - When do I need to use the connect() method?](http://stackoverflow.com/questions/16122999/java-urlconnection-when-do-i-need-to-use-the-connect-method) – Isabelle Jun 10 '16 at 10:21

1 Answers1

1

This is a very broad question. I will try to answer this briefly.

Whenever we say we are making a http request we establish a socket connection with server on port 80 ( port 443 for https). Then we write in the socket following the http protocol and wait for the server to respond.

Here there is a time out which we can set for establishing the connection and then set a time for which we are going to wait for the server to respond.

This is a sync call, that means the thread is going to be blocked till the server responds or the timeout happens.

Arpit Ratan
  • 2,976
  • 1
  • 12
  • 20
  • Thanks for the brief description. But can please suggest , what is inside the connect() function written , so then it start process the connection to the server. – Anshuman Pattnaik Jun 10 '16 at 10:47
  • http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b27/sun/net/www/protocol/http/HttpURLConnection.java#HttpURLConnection.connect%28%29 - You can find detailed information in this link. – Arpit Ratan Jun 10 '16 at 11:07