Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/HttpURLConnection/SetAuthenticator/HTTPTestClient.java
41152 views
1
/*
2
* Copyright (c) 2016, 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
import java.io.IOException;
25
import java.net.Authenticator;
26
import java.net.HttpURLConnection;
27
import java.net.InetSocketAddress;
28
import java.net.Proxy;
29
import java.net.URL;
30
import javax.net.ssl.HttpsURLConnection;
31
32
/**
33
* A simple Http client that connects to the HTTPTestServer.
34
* @author danielfuchs
35
*/
36
public class HTTPTestClient extends HTTPTest {
37
38
public static void connect(HttpProtocolType protocol,
39
HTTPTestServer server,
40
HttpAuthType authType,
41
Authenticator auth)
42
throws IOException {
43
44
InetSocketAddress address = server.getAddress();
45
final URL url = url(protocol, address, "/");
46
final Proxy proxy = proxy(server, authType);
47
48
System.out.println("Client: FIRST request: " + url + " GET");
49
HttpURLConnection conn = openConnection(url, authType, proxy);
50
configure(conn, auth);
51
System.out.println("Response code: " + conn.getResponseCode());
52
String result = new String(conn.getInputStream().readAllBytes(), "UTF-8");
53
System.out.println("Response body: " + result);
54
if (!result.isEmpty()) {
55
throw new RuntimeException("Unexpected response to GET: " + result);
56
}
57
System.out.println("\nClient: NEXT request: " + url + " POST");
58
conn = openConnection(url, authType, proxy);
59
configure(conn, auth);
60
conn.setRequestMethod("POST");
61
conn.setDoOutput(true);
62
conn.setDoInput(true);
63
conn.getOutputStream().write("Hello World!".getBytes("UTF-8"));
64
System.out.println("Response code: " + conn.getResponseCode());
65
result = new String(conn.getInputStream().readAllBytes(), "UTF-8");
66
System.out.println("Response body: " + result);
67
if ("Hello World!".equals(result)) {
68
System.out.println("Test passed!");
69
} else {
70
throw new RuntimeException("Unexpected response to POST: " + result);
71
}
72
}
73
74
private static void configure(HttpURLConnection conn, Authenticator auth)
75
throws IOException {
76
if (auth != null) {
77
conn.setAuthenticator(auth);
78
}
79
if (conn instanceof HttpsURLConnection) {
80
System.out.println("Client: configuring SSL connection");
81
// We have set a default SSLContext so we don't need to do
82
// anything here. Otherwise it could look like:
83
// HttpsURLConnection httpsConn = (HttpsURLConnection)conn;
84
// httpsConn.setSSLSocketFactory(
85
// new SimpleSSLContext().get().getSocketFactory());
86
}
87
}
88
89
}
90
91