Tôi muốn biết những gì đã được thực hiện khi URL.openconnection().
tôi đã làm một số xét nghiệm như thế này:điều gì đã được thực hiện khi URL.openconnection()?
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();
}
khi tôi chạy testConnection ("http://www.google.com"), tất cả mọi thứ là ok.
khi tôi chạy testConnection ("http: //219.09.34.23.1"), "219.09.34.23.1" là một ip ngẫu nhiên có lẽ không tồn tại i đã viết, nó in này:
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
nó có nghĩa là nó đã dành 5ms để chạy openconnection, và đã dành 43ms để tìm thấy nó là một unknownhost, vấn đề của tôi là, những gì đã được thực hiện khi URL.openconnection() là "219.09.34.23.1" là unknownhost? Cảm ơn bạn đã giúp đỡ!
như thế nào? Ý định của bạn là gì? xin vui lòng chỉnh sửa cho một câu hỏi thực sự – timaschew
Giống như những gì .....? –