Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/URLConnection/GetResponseCode.java
38811 views
/*1* Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24*25* @bug 419181526* @summary Check that getResponseCode doesn't throw exception if http27* respone code is >= 400.28*/29import java.net.*;30import java.io.*;3132public class GetResponseCode implements Runnable {3334ServerSocket ss;3536/*37* Our "http" server to return a 40438*/39public void run() {40try {41Socket s = ss.accept();4243PrintStream out = new PrintStream(44new BufferedOutputStream(45s.getOutputStream() ));4647/* send the header */48out.print("HTTP/1.1 404 Not Found\r\n");49out.print("Content-Type: text/html; charset=iso-8859-1\r\n");50out.print("Connection: close\r\n");51out.print("\r\n");52out.print("<HTML>");53out.print("<HEAD><TITLE>404 Not Found</TITLE></HEAD>");54out.print("<BODY>The requested URL was not found.</BODY>");55out.print("</HTML>");56out.flush();5758s.close();59ss.close();60} catch (Exception e) {61e.printStackTrace();62}63}6465GetResponseCode() throws Exception {6667/* start the server */68ss = new ServerSocket(0);69(new Thread(this)).start();7071/* establish http connection to server */72String uri = "http://localhost:" +73Integer.toString(ss.getLocalPort()) +74"/missing.nothtml";75URL url = new URL(uri);7677HttpURLConnection http = (HttpURLConnection)url.openConnection();7879int respCode = http.getResponseCode();8081http.disconnect();8283}8485public static void main(String args[]) throws Exception {86new GetResponseCode();87}8889}909192