Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7778085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:18:27+00:00 2026-06-01T18:18:27+00:00

I am using HTTPURLConnection to connect to a web service. I know how to

  • 0

I am using HTTPURLConnection to connect to a web service. I know how to use HTTPURLConnection but I want to understand how it works. Basically, I want to know the following:

  • On which point does HTTPURLConnection try to establish a connection to the given URL?
  • On which point can I know that I was able to successfully establish a connection?
  • Are establishing a connection and sending the actual request done in one step/method call? What method is it?
  • Can you explain the function of getOutputStream and getInputStream in layman’s term? I notice that when the server I’m trying to connect to is down, I get an Exception at getOutputStream. Does it mean that HTTPURLConnection will only start to establish a connection when I invoke getOutputStream? How about the getInputStream? Since I’m only able to get the response at getInputStream, then does it mean that I didn’t send any request at getOutputStream yet but simply establishes a connection? Do HttpURLConnection go back to the server to request for response when I invoke getInputStream?
  • Am I correct to say that openConnection simply creates a new connection object but does not establish any connection yet?
  • How can I measure the read overhead and connect overhead?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T18:18:29+00:00Added an answer on June 1, 2026 at 6:18 pm
    String message = URLEncoder.encode("my message", "UTF-8");
    
    try {
        // instantiate the URL object with the target URL of the resource to
        // request
        URL url = new URL("http://www.example.com/comment");
    
        // instantiate the HttpURLConnection with the URL object - A new
        // connection is opened every time by calling the openConnection
        // method of the protocol handler for this URL.
        // 1. This is the point where the connection is opened.
        HttpURLConnection connection = (HttpURLConnection) url
                .openConnection();
        // set connection output to true
        connection.setDoOutput(true);
        // instead of a GET, we're going to send using method="POST"
        connection.setRequestMethod("POST");
    
        // instantiate OutputStreamWriter using the output stream, returned
        // from getOutputStream, that writes to this connection.
        // 2. This is the point where you'll know if the connection was
        // successfully established. If an I/O error occurs while creating
        // the output stream, you'll see an IOException.
        OutputStreamWriter writer = new OutputStreamWriter(
                connection.getOutputStream());
    
        // write data to the connection. This is data that you are sending
        // to the server
        // 3. No. Sending the data is conducted here. We established the
        // connection with getOutputStream
        writer.write("message=" + message);
    
        // Closes this output stream and releases any system resources
        // associated with this stream. At this point, we've sent all the
        // data. Only the outputStream is closed at this point, not the
        // actual connection
        writer.close();
        // if there is a response code AND that response code is 200 OK, do
        // stuff in the first if block
        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            // OK
    
            // otherwise, if any other status code is returned, or no status
            // code is returned, do stuff in the else block
        } else {
            // Server returned HTTP error code.
        }
    } catch (MalformedURLException e) {
        // ...
    } catch (IOException e) {
        // ...
    }
    

    The first 3 answers to your questions are listed as inline comments, beside each method, in the example HTTP POST above.

    From getOutputStream:

    Returns an output stream that writes to this connection.

    Basically, I think you have a good understanding of how this works, so let me just reiterate in layman’s terms. getOutputStream basically opens a connection stream, with the intention of writing data to the server. In the above code example “message” could be a comment that we’re sending to the server that represents a comment left on a post. When you see getOutputStream, you’re opening the connection stream for writing, but you don’t actually write any data until you call writer.write("message=" + message);.

    From getInputStream():

    Returns an input stream that reads from this open connection. A SocketTimeoutException can be thrown when reading from the returned input stream if the read timeout expires before data is available for read.

    getInputStream does the opposite. Like getOutputStream, it also opens a connection stream, but the intent is to read data from the server, not write to it. If the connection or stream-opening fails, you’ll see a SocketTimeoutException.

    How about the getInputStream? Since I’m only able to get the response at getInputStream, then does it mean that I didn’t send any request at getOutputStream yet but simply establishes a connection?

    Keep in mind that sending a request and sending data are two different operations. When you invoke getOutputStream or getInputStream url.openConnection(), you send a request to the server to establish a connection. There is a handshake that occurs where the server sends back an acknowledgement to you that the connection is established. It is then at that point in time that you’re prepared to send or receive data. Thus, you do not need to call getOutputStream to establish a connection open a stream, unless your purpose for making the request is to send data.

    In layman’s terms, making a getInputStream request is the equivalent of making a phone call to your friend’s house to say “Hey, is it okay if I come over and borrow that pair of vice grips?” and your friend establishes the handshake by saying, “Sure! Come and get it”. Then, at that point, the connection is made, you walk to your friend’s house, knock on the door, request the vice grips, and walk back to your house.

    Using a similar example for getOutputStream would involve calling your friend and saying “Hey, I have that money I owe you, can I send it to you”? Your friend, needing money and sick inside that you kept it for so long, says “Sure, come on over you cheap bastard”. So you walk to your friend’s house and “POST” the money to him. He then kicks you out and you walk back to your house.

    Now, continuing with the layman’s example, let’s look at some Exceptions. If you called your friend and he wasn’t home, that could be a 500 error. If you called and got a disconnected number message because your friend is tired of you borrowing money all the time, that’s a 404 page not found. If your phone is dead because you didn’t pay the bill, that could be an IOException. (NOTE: This section may not be 100% correct. It’s intended to give you a general idea of what’s happening in layman’s terms.)

    Question #5:

    Yes, you are correct that openConnection simply creates a new connection object but does not establish it. The connection is established when you call either getInputStream or getOutputStream.

    openConnection creates a new connection object. From the URL.openConnection javadocs:

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

    The connection is established when you call openConnection, and the InputStream, OutputStream, or both, are called when you instantiate them.

    Question #6:

    To measure the overhead, I generally wrap some very simple timing code around the entire connection block, like so:

    long start = System.currentTimeMillis();
    log.info("Time so far = " + new Long(System.currentTimeMillis() - start) );
    
    // run the above example code here
    log.info("Total time to send/receive data = " + new Long(System.currentTimeMillis() - start) );
    

    I’m sure there are more advanced methods for measuring the request time and overhead, but this generally is sufficient for my needs.

    For information on closing connections, which you didn’t ask about, see In Java when does a URL connection close?.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What are the differences if I try to connect to an https using HttpURLConnection
I'm working on application which has workflow like this: 1.parsing home page (using HttpURLConnection,
I'd like to use the Java class HttpURLConnection to connect to a particular device
I am using java.net.HttpURLConnection to connect to a website and send some data through
i can establish a connection using HttpUrlConnection. my code below. client = new DefaultHttpClient();
I'm connecting to a webserver with a specific JavaScript. (Using HttpURLConnection atm) What i
Using PyObjC , you can use Python to write Cocoa applications for OS X.
I'm making an android program that retrieves content of a webpage using HttpURLConnection. I'm
I am trying to use java.net to connect to remote URL in Google App
I am using the following website to find elevation: http://gisdata.usgs.gov/xmlwebservices2/elevation_service.asmx/getElevation?X_Value=-78.85834070853889&Y_Value=43.869369104504585&Elevation_Units=METERS&Source_Layer=-1&Elevation_Only=true This provides a results

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.