4

I am using java.net for sending HTTP requests in my Java client and I still can not realize/find how to actually fire the request.

For example I have this code:

Scanner sc = new Scanner(System.in); System.out.println("Deleting subject..."); System.out.println("Subject shortcut (-1 for return):"); String shortcut = sc.next(); if( shortcut.equals("-1") ) return ; try { URL url = new URL( "http://localhost:8080/Server/webresources/subject/delete/"+shortcut ); HttpURLConnection con = (HttpURLConnection)url.openConnection(); con.setDoOutput(true); con.setRequestMethod("DELETE"); BufferedReader br = new BufferedReader( new InputStreamReader( con.getInputStream() ) ); System.out.println( br.readLine() ); }catch( Exception e ) { System.out.println(e.getMessage()); } 

In this code if I do not use these lines:

BufferedReader br = new BufferedReader( new InputStreamReader( con.getInputStream() ) ); System.out.println( br.readLine() ); 

the request is never sent. So in this case the request seems to be trigger by calling for InputStream from connection.

Can anyone explain me how is a HTTP request via java.net fired?

5
  • Possible duplicate of Using java.net.URLConnection to fire and handle HTTP requests Commented Jan 6, 2017 at 14:40
  • 1
    The first answer to that question goes into quite a lot of detail. Basically you're right that the request gets sent when you read from the input stream. Commented Jan 6, 2017 at 14:43
  • 1
    @Michael URLConnection#connect() as written in the topic you have just posted does not work. Commented Jan 6, 2017 at 14:43
  • @Michael What you said is written there but what if I do not want any response? Then I do not want to read and then I also do not want to create a BufferedReader Commented Jan 6, 2017 at 14:44
  • 1
    A lot of this is in the javadocs for the base class: docs.oracle.com/javase/8/docs/api/java/net/URLConnection.html (Though it's not written super clearly) Commented Jan 6, 2017 at 14:44

2 Answers 2

5

From the documentation, a HttpURLConnection will connect either if you call connect(), or if you call an operation that depends on being connected, like getInputStream() .

Opens a communications link to the resource referenced by this URL, if such a connection has not already been established. If the connect method is called when the connection has already been opened (indicated by the connected field having the value true), the call is ignored.

URLConnection objects go through two phases: first they are created, then they are connected. After being created, and before being connected, various options can be specified (e.g., doInput and UseCaches). After connecting, it is an error to try to set them. Operations that depend on being connected, like getContentLength, will implicitly perform the connection, if necessary.

However, several topics indicate that connect() won't commit the actual request, but getInputStream() (and most likely any method reading the server's response e.g getResponseCode() ), will :

Java URLConnection - When do I need to use the connect() method?

Why does HttpURLConnection not send the HTTP request

How to send PUT, DELETE HTTP request in HttpURLConnection?

Sign up to request clarification or add additional context in comments.

4 Comments

Unfortunately, if I call connect(), nohing happens.
@scarface : this is strange, but you must be right since the comments from the following topic state the same thing : stackoverflow.com/questions/1051004/…
I am going to check those topics. Anyway it is weird getting InputStream when, in some cases, I do not actually get any output from server. By the way also when I ask for a response number, the request is sent.
@scarface : yes it seems that in any case, you have to read something from the server (inputstream, response code, content length...). At least it makes sense to read the response code whatever your request is .
0

A new instance of URLConnection is created every time when invoking the URLStreamHandler.openConnection(URL) method of the protocol handler for this URL.

It should be noted that a URLConnection instance does not establish the actual network connection on creation. This will happen only when calling URLConnection.connect(). see javadoc

 String url = "http://example.com"; URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("GET"); //add request header con.setRequestProperty("User-Agent", USER_AGENT); int responseCode = con.getResponseCode(); 

1 Comment

This is cool but it did not answer my question. Instead of getting InputStream, I get responseCode and that's all. That is not an explanation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.