java - URL.openconnection() 时做了什么?

标签 java url httpurlconnection

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

当我运行 testConnection("http://www.google.com") 时,一切正常。
当我运行 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

意思是运行openconnection用了5ms,发现是unknownhost用了43ms,我的问题是,当URL.openconnection() as "219.09.34.23.1"is unknownhost时做了什么? 感谢您的帮助!

最佳答案

如果您阅读 javadocs for URL.openConnection() ,你会发现:

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

更新

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

关于java - URL.openconnection() 时做了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10916219/

相关文章:

javascript - 使用 Java 从网络下载生成的图像

android - 在 Android 中发布大文件

java - 即使服务器已启动,如何解决 java net ConnectException 连接被拒绝连接

java - 在 Maven 存储库中查找 Oracle JDBC 驱动程序

java - 如果连续写入日志文件,是否可以以编程方式打开日志文件?

css - 无法使用 sass-loader (Webpack) 加载背景图像

java - Jersey/Java Web 服务中的 URL 编码

java 。从包中获取 URL

java - 更改 Ribbon Application MenuEntry Primary (Flamingo) 的背景颜色

java - 遍历大型 7*7 矩阵内的 3* 3 子矩阵时绑定(bind)异常错误的数组索引