简体   繁体   中英

How HttpURLConnection get's its inputStream instance?

I am curious how HttpURLConnection gets hold of the connection's input stream. I checked the implementation of the parent abstract class URLConnection and found the method

 /**
     * Returns an {@code InputStream} for reading data from the resource pointed by
     * this {@code URLConnection}. It throws an UnknownServiceException by
     * default. This method must be overridden by its subclasses.
     *
     * @return the InputStream to read data from.
     * @throws IOException
     *             if no InputStream could be created.
     */
    public InputStream getInputStream() throws IOException {
        throw new UnknownServiceException("Does not support writing to the input stream");
    }
  1. HttpURLConnection which extends URLConnection doesnt override this method. So was wondering how the inputstream reference is obtained?

  2. Something related to this.... When exactly does the http request go to of the client to server? Is it when you call getInputStream() ? or as soon as openConnection() is called?

Thanks in advance AK

If you print the runtime type of the HttpURLConnection object it will be of type org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl on Android. That is where your implementation of getInputStream() is.

For reference, if you were to print the runtime type of the same object in an Oracle JRE, the runtime type would be sun.net.www.protocol.http.HttpURLConnection

Note: I listed the Android version first because the question was previously tagged as an Android question.

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