Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/HttpURLConnection/HttpResponseCode.java
38811 views
1
/*
2
* Copyright (c) 2001, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/**
25
* @test
26
* @bug 4473092
27
* @summary Method throws IOException when object should be returned
28
*/
29
import java.net.*;
30
import java.io.*;
31
32
public class HttpResponseCode implements Runnable {
33
ServerSocket ss;
34
/*
35
* Our "http" server
36
*/
37
public void run() {
38
try {
39
Socket s = ss.accept();
40
41
BufferedReader in = new BufferedReader(
42
new InputStreamReader(s.getInputStream()) );
43
String req = in.readLine();
44
45
PrintStream out = new PrintStream(
46
new BufferedOutputStream(
47
s.getOutputStream() ));
48
49
50
/* send the header */
51
out.print("HTTP/1.1 403 Forbidden\r\n");
52
out.print("\r\n");
53
out.flush();
54
55
s.close();
56
ss.close();
57
} catch (Exception e) {
58
e.printStackTrace();
59
}
60
}
61
62
HttpResponseCode() throws Exception {
63
/* start the server */
64
ss = new ServerSocket(0);
65
(new Thread(this)).start();
66
67
/* establish http connection to server */
68
String url = "http://localhost:" +
69
Integer.toString(ss.getLocalPort()) +
70
"/missing.nothtml";
71
URLConnection uc = new URL(url).openConnection();
72
int respCode1 = ((HttpURLConnection)uc).getResponseCode();
73
((HttpURLConnection)uc).disconnect();
74
int respCode2 = ((HttpURLConnection)uc).getResponseCode();
75
if (respCode1 != 403 || respCode2 != 403) {
76
throw new RuntimeException("Testing Http response code failed");
77
}
78
}
79
80
public static void main(String args[]) throws Exception {
81
new HttpResponseCode();
82
}
83
}
84
85