test/jdk/java/net/URLConnection/GetResponseCode.java
changeset 55645 3203e857fa71
parent 47216 71c04702a3d5
equal deleted inserted replaced
55644:556313991cac 55645:3203e857fa71
     1 /*
     1 /*
     2  * Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    20  * or visit www.oracle.com if you need additional information or have any
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /**
    24 /**
    25  *
    25  * @test
    26  * @bug 4191815
    26  * @bug 4191815
       
    27  * @library /test/lib
    27  * @summary Check that getResponseCode doesn't throw exception if http
    28  * @summary Check that getResponseCode doesn't throw exception if http
    28  *          respone code is >= 400.
    29  *          respone code is >= 400.
    29  */
    30  */
    30 import java.net.*;
    31 import java.net.*;
    31 import java.io.*;
    32 import java.io.*;
       
    33 
       
    34 import jdk.test.lib.net.URIBuilder;
    32 
    35 
    33 public class GetResponseCode implements Runnable {
    36 public class GetResponseCode implements Runnable {
    34 
    37 
    35     ServerSocket ss;
    38     ServerSocket ss;
    36 
    39 
    54             out.print("<HEAD><TITLE>404 Not Found</TITLE></HEAD>");
    57             out.print("<HEAD><TITLE>404 Not Found</TITLE></HEAD>");
    55             out.print("<BODY>The requested URL was not found.</BODY>");
    58             out.print("<BODY>The requested URL was not found.</BODY>");
    56             out.print("</HTML>");
    59             out.print("</HTML>");
    57             out.flush();
    60             out.flush();
    58 
    61 
       
    62             /*
       
    63              * Sleep added to avoid connection reset
       
    64              * on the client side
       
    65              */
       
    66             Thread.sleep(1000);
    59             s.close();
    67             s.close();
    60             ss.close();
    68             ss.close();
    61         } catch (Exception e) {
    69         } catch (Exception e) {
    62             e.printStackTrace();
    70             e.printStackTrace();
    63         }
    71         }
    64     }
    72     }
    65 
    73 
    66     GetResponseCode() throws Exception {
    74     GetResponseCode() throws Exception {
    67 
    75 
    68         /* start the server */
    76         /* start the server */
    69         ss = new ServerSocket(0);
    77         ss = new ServerSocket();
       
    78         ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
    70         (new Thread(this)).start();
    79         (new Thread(this)).start();
    71 
    80 
    72         /* establish http connection to server */
    81         /* establish http connection to server */
    73         String uri = "http://localhost:" +
    82         URL url = URIBuilder.newBuilder()
    74                      Integer.toString(ss.getLocalPort()) +
    83                 .scheme("http")
    75                      "/missing.nothtml";
    84                 .loopback()
    76         URL url = new URL(uri);
    85                 .port(ss.getLocalPort())
       
    86                 .path("/missing.nohtml")
       
    87                 .toURL();
    77 
    88 
    78         HttpURLConnection http = (HttpURLConnection)url.openConnection();
    89         HttpURLConnection http = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
    79 
    90 
    80         int respCode = http.getResponseCode();
    91         int respCode = http.getResponseCode();
    81 
    92 
    82         http.disconnect();
    93         http.disconnect();
    83 
    94