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