简体   繁体   中英

java.net How to fire an HTTP request

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?

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 (eg, 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 eg 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?

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM