本文介绍了URL.openconnection()时做了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道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();
}

当我运行testConnection("http://www.google.com")时,一切正常.
当我运行testConnection("http://219.09.34.23.1")时,"219.09.34.23.1"是我写的一个随机IP可能不存在,它打印此:

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

这意味着它花费了5毫秒来运行openconnection,并花费了43毫秒来发现它是一个未知主机,我的问题是,当URL.openconnection()作为"219.09.34.23.1"时,unknownhost是怎么做的?感谢您的帮助!

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!

推荐答案

如果您阅读了 URL.openConnection() 的javadocs,您将找到:

If you read the javadocs for URL.openConnection(), you'll find:

每次调用时都会创建一个URLConnection的新实例 协议的URLStreamHandler.openConnection(URL)方法 此URL的处理程序.

A new instance of URLConnection is created every time when invoking the URLStreamHandler.openConnection(URL) method of the protocol handler for this URL.

应注意,URLConnection实例未建立 创建时的实际网络连接.这只会在以下情况下发生 呼叫URLConnection.connect().

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

更新

您在随机IP"中使用的IP无效;它应该由4个字节组成,而不是5个字节.43ms可能用于:(1)在非IP ip上进行DNS查找(2)打印堆栈跟踪.

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.

这篇关于URL.openconnection()时做了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 13:51