author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 44854 | jdk/test/java/net/httpclient/http2/Timeout.java@5a486e0acd29 |
child 48083 | b1c1b4ef4be2 |
child 55763 | 634d8e14c172 |
permissions | -rw-r--r-- |
37906
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
1 |
/* |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
2 |
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
4 |
* |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
8 |
* |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
13 |
* accompanied this code). |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
14 |
* |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
18 |
* |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
21 |
* questions. |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
22 |
*/ |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
23 |
|
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
24 |
import java.io.File; |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
25 |
import java.io.IOException; |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
26 |
import java.net.URI; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
27 |
import jdk.incubator.http.HttpClient; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
28 |
import jdk.incubator.http.HttpRequest; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
29 |
import jdk.incubator.http.HttpResponse; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
30 |
import jdk.incubator.http.HttpTimeoutException; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
31 |
import java.time.Duration; |
37906
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
32 |
import java.util.concurrent.TimeUnit; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
33 |
import java.util.concurrent.CompletionException; |
37906
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
34 |
import javax.net.ssl.SSLServerSocket; |
44854
5a486e0acd29
8175814: Update default HttpClient protocol version and optional request version
michaelm
parents:
42460
diff
changeset
|
35 |
import javax.net.ssl.SSLParameters; |
37906
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
36 |
import javax.net.ssl.SSLServerSocketFactory; |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
37 |
import javax.net.ssl.SSLSocket; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
38 |
import static jdk.incubator.http.HttpRequest.BodyProcessor.fromString; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
39 |
import static jdk.incubator.http.HttpResponse.BodyHandler.asString; |
37906
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
40 |
|
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
41 |
/* |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
42 |
* @test |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
43 |
* @bug 8156710 |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
44 |
* @summary Check if HttpTimeoutException is thrown if a server doesn't reply |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
45 |
* @run main/othervm Timeout |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
46 |
*/ |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
47 |
public class Timeout { |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
48 |
|
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
49 |
private static final int RANDOM_PORT = 0; |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
50 |
private static final int TIMEOUT = 3 * 1000; // in millis |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
51 |
private static final String KEYSTORE = System.getProperty("test.src") |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
52 |
+ File.separator + "keystore.p12"; |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
53 |
private static final String PASSWORD = "password"; |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
54 |
|
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
55 |
// indicates if server is ready to accept connections |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
56 |
private static volatile boolean ready = false; |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
57 |
|
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
58 |
public static void main(String[] args) throws Exception { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
59 |
test(false); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
60 |
test(true); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
61 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
62 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
63 |
public static void test(boolean async) throws Exception { |
37906
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
64 |
System.setProperty("javax.net.ssl.keyStore", KEYSTORE); |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
65 |
System.setProperty("javax.net.ssl.keyStorePassword", PASSWORD); |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
66 |
System.setProperty("javax.net.ssl.trustStore", KEYSTORE); |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
67 |
System.setProperty("javax.net.ssl.trustStorePassword", PASSWORD); |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
68 |
|
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
69 |
SSLServerSocketFactory factory = |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
70 |
(SSLServerSocketFactory) SSLServerSocketFactory.getDefault(); |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
71 |
|
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
72 |
try (SSLServerSocket ssocket = |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
73 |
(SSLServerSocket) factory.createServerSocket(RANDOM_PORT)) { |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
74 |
|
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
75 |
// start server |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
76 |
Thread server = new Thread(() -> { |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
77 |
while (true) { |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
78 |
System.out.println("server: ready"); |
44854
5a486e0acd29
8175814: Update default HttpClient protocol version and optional request version
michaelm
parents:
42460
diff
changeset
|
79 |
SSLParameters params = ssocket.getSSLParameters(); |
5a486e0acd29
8175814: Update default HttpClient protocol version and optional request version
michaelm
parents:
42460
diff
changeset
|
80 |
params.setApplicationProtocols(new String[]{"h2"}); |
5a486e0acd29
8175814: Update default HttpClient protocol version and optional request version
michaelm
parents:
42460
diff
changeset
|
81 |
ssocket.setSSLParameters(params); |
37906
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
82 |
ready = true; |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
83 |
try (SSLSocket socket = (SSLSocket) ssocket.accept()) { |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
84 |
|
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
85 |
// just read forever |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
86 |
System.out.println("server: accepted"); |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
87 |
while (true) { |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
88 |
socket.getInputStream().read(); |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
89 |
} |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
90 |
} catch (IOException e) { |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
91 |
// ignore exceptions on server side |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
92 |
System.out.println("server: exception: " + e); |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
93 |
} |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
94 |
} |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
95 |
}); |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
96 |
server.setDaemon(true); |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
97 |
server.start(); |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
98 |
|
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
99 |
// wait for server is ready |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
100 |
do { |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
101 |
Thread.sleep(1000); |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
102 |
} while (!ready); |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
103 |
|
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
104 |
String uri = "https://localhost:" + ssocket.getLocalPort(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
105 |
if (async) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
106 |
System.out.println(uri + ": Trying to connect asynchronously"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
107 |
connectAsync(uri); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
108 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
109 |
System.out.println(uri + ": Trying to connect synchronously"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
110 |
connect(uri); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
111 |
} |
37906
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
112 |
} |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
113 |
} |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
114 |
|
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
115 |
private static void connect(String server) throws Exception { |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
116 |
try { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
117 |
HttpClient client = HttpClient.newBuilder() |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
118 |
.version(HttpClient.Version.HTTP_2) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
119 |
.build(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
120 |
HttpRequest request = HttpRequest.newBuilder(new URI(server)) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
121 |
.timeout(Duration.ofMillis(TIMEOUT)) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
122 |
.POST(fromString("body")) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
123 |
.build(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
124 |
HttpResponse<String> response = client.send(request, asString()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
125 |
System.out.println("Received unexpected reply: " + response.statusCode()); |
37906
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
126 |
throw new RuntimeException("unexpected successful connection"); |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
127 |
} catch (HttpTimeoutException e) { |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
128 |
System.out.println("expected exception: " + e); |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
129 |
} |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
130 |
} |
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
131 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
132 |
private static void connectAsync(String server) throws Exception { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
133 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
134 |
HttpClient client = HttpClient.newBuilder() |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
135 |
.version(HttpClient.Version.HTTP_2) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
136 |
.build(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
137 |
HttpRequest request = HttpRequest.newBuilder(new URI(server)) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
138 |
.timeout(Duration.ofMillis(TIMEOUT)) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
139 |
.POST(fromString("body")) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
140 |
.build(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
141 |
HttpResponse<String> response = client.sendAsync(request, asString()).join(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
142 |
System.out.println("Received unexpected reply: " + response.statusCode()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
143 |
throw new RuntimeException("unexpected successful connection"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
144 |
} catch (CompletionException e) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
145 |
if (e.getCause() instanceof HttpTimeoutException) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
146 |
System.out.println("expected exception: " + e.getCause()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
147 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
148 |
throw new RuntimeException("Unexpected exception received: " + e.getCause(), e); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
149 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
150 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
151 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
37906
diff
changeset
|
152 |
|
37906
c2ea5f68c2ff
8156710: HttpTimeoutException should be thrown if server doesn't respond
asmotrak
parents:
diff
changeset
|
153 |
} |