我很好奇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");
}
HttpURLConnection的延伸URLConnection的犯规重写此方法。所以想知道如何获得输入流参考?
与此有关的东西....什么时候http请求转到客户端到服务器?当你调用getInputStream()吗?或只要openConnection()被调用?
在此先感谢 AK
谢谢开发。我正在使用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
它是Java JDK的一部分,在rt.jar中。 – EJP
@AKh运行时环境将始终提供实现。 – Dev