duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2001 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 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 |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * |
| 26 | * @bug 4191815 |
| 27 | * @summary Check that getResponseCode doesn't throw exception if http |
| 28 | * respone code is >= 400. |
| 29 | */ |
| 30 | import java.net.*; |
| 31 | import java.io.*; |
| 32 | |
| 33 | public class GetResponseCode implements Runnable { |
| 34 | |
| 35 | ServerSocket ss; |
| 36 | |
| 37 | /* |
| 38 | * Our "http" server to return a 404 |
| 39 | */ |
| 40 | public void run() { |
| 41 | try { |
| 42 | Socket s = ss.accept(); |
| 43 | |
| 44 | PrintStream out = new PrintStream( |
| 45 | new BufferedOutputStream( |
| 46 | s.getOutputStream() )); |
| 47 | |
| 48 | /* send the header */ |
| 49 | out.print("HTTP/1.1 404 Not Found\r\n"); |
| 50 | out.print("Content-Type: text/html; charset=iso-8859-1\r\n"); |
| 51 | out.print("Connection: close\r\n"); |
| 52 | out.print("\r\n"); |
| 53 | out.print("<HTML>"); |
| 54 | out.print("<HEAD><TITLE>404 Not Found</TITLE></HEAD>"); |
| 55 | out.print("<BODY>The requested URL was not found.</BODY>"); |
| 56 | out.print("</HTML>"); |
| 57 | out.flush(); |
| 58 | |
| 59 | s.close(); |
| 60 | ss.close(); |
| 61 | } catch (Exception e) { |
| 62 | e.printStackTrace(); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | GetResponseCode() throws Exception { |
| 67 | |
| 68 | /* start the server */ |
| 69 | ss = new ServerSocket(0); |
| 70 | (new Thread(this)).start(); |
| 71 | |
| 72 | /* establish http connection to server */ |
| 73 | String uri = "http://localhost:" + |
| 74 | Integer.toString(ss.getLocalPort()) + |
| 75 | "/missing.nothtml"; |
| 76 | URL url = new URL(uri); |
| 77 | |
| 78 | HttpURLConnection http = (HttpURLConnection)url.openConnection(); |
| 79 | |
| 80 | int respCode = http.getResponseCode(); |
| 81 | |
| 82 | http.disconnect(); |
| 83 | |
| 84 | } |
| 85 | |
| 86 | public static void main(String args[]) throws Exception { |
| 87 | new GetResponseCode(); |
| 88 | } |
| 89 | |
| 90 | } |