openconnection java,URL.openconnection()做了什么?

I want to know what has been done when 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.

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:

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.(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?

Thanks for any help!

解决方案

Returns a URLConnection instance that represents a connection to the

remote object referred to by the URL.

A new instance of URLConnection is created every time when invoking

the URLStreamHandler.openConnection(URL) method of the protocol

handler for this URL.

It should be noted that a URLConnection instance does not establish

the actual network connection on creation. This will happen only when

calling URLConnection.connect().

Update

The IP you used in your "random ip" is not valid; 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值