[英]what has been done when URL.openconnection()?
I want to know what has been done when URL.openconnection(). 我想知道在URL.openconnection()时做了什么。
i have done some test like this: 我做了一些像这样的测试:
public static void main(String[] args) {
// testConnection("http://www.google.com");
testConnection("http://219.09.34.23.1");
}
private static void testConnection(final String _url) {
new Thread(new Runnable() {
String strurl = _url;
long starttime = 0;
long endtime = 0;
public void run() {
try {
System.out.println("open:" + strurl);
starttime = System.currentTimeMillis();
System.out.println("starttime:" + starttime);
URL url = new URL(strurl);
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
endtime = System.currentTimeMillis();
System.out.println("openConnection endtime:" + endtime);
System.out
.println("spend:" + (endtime - starttime) + " ms");
conn.connect();
endtime = System.currentTimeMillis();
System.out.println("connect endtime2:" + endtime);
System.out
.println("spend:" + (endtime - starttime) + " ms");
conn.getResponseCode();
endtime = System.currentTimeMillis();
System.out.println("endtime3:" + endtime);
System.out
.println("spend:" + (endtime - starttime) + " ms");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
endtime = System.currentTimeMillis();
System.out.println("MalformedURLException endtime:"
+ endtime);
System.out
.println("spend:" + (endtime - starttime) + " ms");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
endtime = System.currentTimeMillis();
System.out.println(" IOException endtime:" + endtime);
System.out
.println("spend:" + (endtime - starttime) + " ms");
}
}
}).start();
}
when i run testConnection("http://www.google.com"), all things are ok. 当我运行testConnection(“http://www.google.com”)时,一切正常。
when i run testConnection("http://219.09.34.23.1"), "219.09.34.23.1" is a random ip maybe not exist i wrote, it print this: 当我运行testConnection(“http://219.09.34.23.1”),“219.09.34.23.1”是一个随机的ip可能不存在我写的,它打印这个:
open:http://219.09.34.23.1
starttime:1338978920350
openconnection endtime:1338978920355
spend:5 ms
java.net.UnknownHostException: 219.09.34.23.1
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at Main$1.run(Main.java:37)
at java.lang.Thread.run(Unknown Source)
IOException endtime:1338978920393
spend:43 ms
it means it spent 5ms to run openconnection, and spent 43ms to find it is a unknownhost, my problem is, what has been done when URL.openconnection() as "219.09.34.23.1" is unknownhost? 这意味着花了5ms来运行openconnection,花了43ms来发现它是一个未知的主机,我的问题是,当URL.openconnection()为“219.09.34.23.1”是未知主机时,做了什么? Thanks for any help!
谢谢你的帮助!
If you read the javadocs for URL.openConnection()
, you'll find: 如果你读了
URL.openConnection()
的javadocs ,你会发现:
Returns a
URLConnection
instance that represents a connection to the remote object referred to by the URL.返回一个
URLConnection
实例,该实例表示与URL引用的远程对象的连接。A new instance of
URLConnection
is created every time when invoking theURLStreamHandler.openConnection(URL)
method of the protocol handler for this URL.每次调用此URL的协议处理程序的
URLStreamHandler.openConnection(URL)
方法时,都会创建一个新的URLConnection
实例。It should be noted that a
URLConnection
instance does not establish the actual network connection on creation.应该注意,
URLConnection
实例在创建时不建立实际的网络连接。 This will happen only when callingURLConnection.connect()
.只有在调用
URLConnection.connect()
时才会发生这种情况。
Update 更新
The IP you used in your "random ip" is not valid; 您在“随机ip”中使用的IP无效; it should consist of 4 octets, not 5. The 43ms is probably for: (1) doing a DNS lookup on the non-IP ip (2) printing the stack trace.
它应该由4个八位字节组成,而不是5个。这43ms可能用于:(1)在非IP ip上进行DNS查找(2)打印堆栈跟踪。
Open connection is not like connect 打开连接不像连接
openconnection
does not let you download URL content that's for sure. openconnection
不允许您下载确定的URL内容。 You have to call connect
. 你必须打电话给
connect
。 But not really, the following will explain: 但不是真的,以下将解释:
You are not always required to explicitly call the connect
method to initiate the connection. 您并不总是需要显式调用
connect
方法来启动连接。 Operations that depend on being connected, like getInputStream
, getOutputStream
, etc, will implicitly perform the connection, if necessary. 依赖于连接的操作(如
getInputStream
, getOutputStream
等)将在必要时隐式执行连接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.