java.net包中的HttpURLConnection类
有一个抽象方法disconnect(),它的直接已知子类: HttpsURLConnection 没有实现抽象方法。
为什么我们可以直接调用disconnect()方法,而且这个抽象方法是在哪个类中实现的?
有一个抽象方法disconnect(),它的直接已知子类: HttpsURLConnection 没有实现抽象方法。
为什么我们可以直接调用disconnect()方法,而且这个抽象方法是在哪个类中实现的?
See http://www.w3.org/pub/WWW/Protocols/ and RFC 2818 for more details on the https specificationThis class uses HostnameVerifier and SSLSocketFactory. There are default implementations defined for both classes
在API中可以看到这样一句解释:
该类用了主机验证器和加密套接字工厂,这两个东东已经默认实现了两个类
{
return handler.openConnection(this);
}但是它并没有实现disconnect方法呀,怎么回事?。谢谢了.
transient URLStreamHandler handler;
public URLConnection openConnection(Proxy proxy)
throws java.io.IOException {
if (proxy == null) {
throw new IllegalArgumentException("proxy can not be null");
}
SecurityManager sm = System.getSecurityManager();
if (proxy.type() != Proxy.Type.DIRECT && sm != null) {
InetSocketAddress epoint = (InetSocketAddress) proxy.address();
if (epoint.isUnresolved())
sm.checkConnect(epoint.getHostName(), epoint.getPort());
else
sm.checkConnect(epoint.getAddress().getHostAddress(),
epoint.getPort());
}
return handler.openConnection(this, proxy);
}
而URLStreamHandler类中的openConnection()方法:
protected URLConnection openConnection(URL u, Proxy p) throws IOException {
throw new UnsupportedOperationException("Method not implemented.");
}
他们最后也没有说明怎么去实现disconnection()方法的啊?
高手快出现。
HttpURLConnection conn = (HttpURLConnection)new URL("http://www.google.com").openConnection();
System.out.println(conn.getClass());输出sun.net.www.protocol.http.HttpURLConnection
你可以再用JAD之类的反编译工具看看它的实现方法。
谢谢。具体点说明
程序的源代码也没有引入sun.net.www.protocol.http包。反而是引入的java.net包。谢谢回答。
import java.net.*;public class he
{
public static void main(String args[]) throws Exception
{
URL u1 = new URL("http://www.hao123.com");
HttpURLConnection conn = (HttpURLConnection)u1.openConnection();
//conn.disconnect();
System.out.println(conn.getClass());
}
}
按照chDw的说法,如果是new URL("http://www.google.com").openConnection()的方式来获得java.net.HttpURLConnection,则其实现类为sun.net.www.protocol.http.HttpURLConnection(或者其父类)。一般来说,我们不用关心谁在实现它,抽象/接口,本来就是为了屏蔽这种相关性的。
java.net包下也有一个HttpURLConnection类,程序为什么不是用的它,而是用的sun.net.www.protocol.http.HttpURLConnection类了?
程序的源代码也没有引入sun.net.www.protocol.http包。反而是引入的java.net包。谢谢回答。
import java.net.*; public class he
{
public static void main(String args[]) throws Exception
{
URL u1 = new URL("http://www.hao123.com");
HttpURLConnection conn = (HttpURLConnection)u1.openConnection();
//conn.disconnect();
System.out.println(conn.getClass());
}
}
而实现有多种方法。如果你用URL.openConnection();则实际使用的就是sun.net.www.protocol.http.HttpURLConnection
在其他情况下,不一定是这个类的。建议你去多学习一下有关接口/多态的知识。
....public class HttpURLConnection extends java.net.HttpURLConnection....
public void disconnect()
{
responseCode = -1;
if(pi != null)
{
pi.finishTracking();
pi = null;
}
if(http != null)
{
if(inputStream != null)
{
HttpClient httpclient = http;
boolean flag = httpclient.isKeepingAlive();
try
{
inputStream.close();
}
catch(IOException ioexception) { }
if(flag)
httpclient.closeIdleConnection();
} else
{
http.setDoNotRetry(true);
http.closeServer();
}
http = null;
connected = false;
}
cachedInputStream = null;
if(cachedHeaders != null)
cachedHeaders.reset();
}
....
我终于知道在那实现的了,和你们分享一下,呵呵。谢谢咯。