2013-08-06 206 views
0

我很好奇HttpURLConnection如何获取连接的输入流。我查了父抽象类URLConnection的实施,发现该方法HttpURLConnection如何获得它的inputStream实例?

/** 
    * 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的延伸URLConnection的犯规重写此方法。所以想知道如何获得输入流参考?

  2. 与此有关的东西....什么时候http请求转到客户端到服务器?当你调用getInputStream()吗?或只要openConnection()被调用?

在此先感谢 AK

回答

3

如果打印HttpURLConnection对象的运行时类型将是org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl类型的Android上。这就是您执行getInputStream()的地方。

仅供参考,如果你要打印在Oracle JRE同一个对象的运行时类型,运行时类型将是sun.net.www.protocol.http.HttpURLConnection

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

+0

谢谢开发。我正在使用java.net.HttpURLConnection,正如你所说的它的sun.net.www.protocol.http.HttpURLConnection的实例....但我不认为这是包含在Java SDK中的东西。这是否需要作为外部jar导入?我看到它是OpenJDK的一部分。 http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/sun/net/www/protocol/http/HttpURLConnection.java – AKh

+2

它是Java JDK的一部分,在rt.jar中。 – EJP

+1

@AKh运行时环境将始终提供实现。 – Dev