After which methods call java send real request to server?

For example, I have the java code below:

URL u = new URL("http://google.com");  
URLConnection c = u.openConnection();  
InputStream s = c.getInputStream();  
int i;  
while ((i = s.read()) != -1) {  
     //do something here  
}  

And I cant understand, in which moment (after which method call) java sends the actual request to the server?

Answers


THe openConnection() method of the URL class, just returns you a new instance of HttpUrlConnection(because your protocol is http). The actual connection doesent happen until you call the getInputStream() or getOutputStream() methods. It's on the call of these methods that connect() is called and the google server receives the connection. And because the protocol is http, the server would return you the google home page, which you are reading using s.read(). (it should give you the html page of google home page)


It happens during the openConnection() method. You can tell this by reading its documentation

A new connection is opened every time by calling the openConnection method of the protocol handler for this URL.


When you call openConnection it is actually trying to create connection with specified url and in case it fails then throws IOException.

openConnection - new connection is opened every time by calling the openConnection method of the protocol handler for this URL.


Need Your Help

Delphi: PostThreadMessage & PeekMessage not working

multithreading delphi delphi-2009

When my main application (Delphi 2009) terminates, I want it to signal my threads (timers, TDataModules with ADO Connections, SMTP etc) to dispose gracefully.

SQL Foreign keys not auto incrementing

sql auto-increment javadb

When I comment out every call of the foreign keys the code runs, creates the tables correctly and fills them with the incrementing id, but when I call the foreign keys in it just breaks and the id ...