HttpURLConnection如何得到它的inputStream实例?

时间:2013-08-06 00:13:07

标签: java http url inputstream httpurlconnection

我很好奇HttpURLConnection如何获取连接的输入流。我检查了父抽象类URLConnection的实现,并找到了方法

 /**
     * 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. 扩展URLConnection的HttpURLConnection不会覆盖此方法。那么想知道如何获得输入流参考?

  2. 与此相关的内容...... http请求到底是从客户端发送到服务器的?是在你调用getInputStream()时?或者一旦调用openConnection()?

  3. 提前致谢 AK

1 个答案:

答案 0 :(得分:3)

如果您打印HttpURLConnection对象的运行时类型,它将在Android上为org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl类型。这就是getInputStream()的实现。

作为参考,如果要在Oracle JRE中打印同一对象的运行时类型,则运行时类型为sun.net.www.protocol.http.HttpURLConnection

注意: 我首先列出了Android版本,因为此问题以前被标记为Android问题。