private static void connect(String server) throws Exception {
try {
HttpClient.create()
.version(HttpClient.Version.HTTP_2)
.build()
.request(new URI(server))
.timeout(TimeUnit.MILLISECONDS, TIMEOUT)
.body(HttpRequest.fromString("body"))
.GET()
.response()
.body(HttpResponse.asString());
throw new RuntimeException("unexpected successful connection");
} catch (HttpTimeoutException e) {
System.out.println("expected exception: " + e);
}
}
java类java.net.http.HttpResponse的实例源码
Timeout.java 文件源码
项目:openjdk9
阅读 27
收藏 0
点赞 0
评论 0
RequestBodyTest.java 文件源码
项目:openjdk9
阅读 20
收藏 0
点赞 0
评论 0
static void requestBodyTypes(final String target,
final String requestType,
final String responseType,
final boolean isAsync)
throws Exception
{
System.out.println("Running test_request_body_type " + requestType +
" and response type " + responseType + " and sync=" + isAsync);
URI uri = new URI(target);
byte buf[];
String filename = smallFile.toFile().getAbsolutePath();
String fileContents = HttpUtils.getFileContent(filename);
buf = fileContents.getBytes();
HttpRequest.Builder builder = HttpUtils.getHttpRequestBuilder(client,
requestType,
uri);
HttpResponse response;
if (!isAsync) {
response = builder.GET().response();
} else {
response = builder.GET().responseAsync().join();
}
HttpUtils.checkResponse(response, requestType, responseType);
System.out.println("OK");
}
TLSConnection.java 文件源码
项目:openjdk9
阅读 21
收藏 0
点赞 0
评论 0
private static void connect(String uriString, SSLParameters sslParameters)
throws URISyntaxException, IOException, InterruptedException {
String body = HttpClient.create()
.sslParameters(sslParameters)
.version(HttpClient.Version.HTTP_2)
.build()
.request(new URI(uriString))
.body(HttpRequest.fromString("body"))
.GET()
.response()
.body(HttpResponse.asString());
System.out.println("Response: " + body);
}
SplitResponse.java 文件源码
项目:openjdk9
阅读 18
收藏 0
点赞 0
评论 0
public static void main(String[] args) throws Exception {
server = new Server(0);
URI uri = new URI(server.getURL());
server.start();
HttpRequest request;
HttpResponse r;
CompletableFuture<HttpResponse> cf1;
for (int i=0; i<responses.length; i++) {
cf1 = HttpRequest.create(uri)
.GET()
.responseAsync();
String body = responses[i];
Server.Connection c = server.activity();
sendSplitResponse(response(body), c);
r = cf1.get();
if (r.statusCode()!= 200)
throw new RuntimeException("Failed");
String rxbody = r.body(HttpResponse.asString());
System.out.println("received " + rxbody);
if (!rxbody.equals(body))
throw new RuntimeException("Failed");
c.close();
}
HttpClient.getDefault().executorService().shutdownNow();
System.out.println("OK");
}
TimeNetworkProvider.java 文件源码
项目:Java-9-Jigsaw-Example
阅读 27
收藏 0
点赞 0
评论 0
@Override
public String now() {
try {
return HttpClient.getDefault()
.request(URI.create("http://www.timeapi.org/utc/now"))
.header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36")
.GET()
.response()
.body(HttpResponse.asString());
} catch (IOException | InterruptedException ex) {
throw new RuntimeException("Network error");
}
}
HttpUtils.java 文件源码
项目:openjdk9
阅读 32
收藏 0
点赞 0
评论 0
public static void checkResponse(final HttpResponse response,
String requestType,
final String responseType)
throws IOException
{
String filename = smallFile.toFile().getAbsolutePath();
String fileContents = HttpUtils.getFileContent(filename);
if (requestType.equals("byteArray_offset")) {
fileContents = fileContents.substring(DEFAULT_OFFSET,
DEFAULT_OFFSET + DEFAULT_LENGTH);
}
byte buf[] = fileContents.getBytes();
String responseBody;
switch (responseType) {
case "string":
responseBody = response.body(asString());
if (!responseBody.equals(fileContents)) {
throw new RuntimeException();
}
break;
case "byteArray":
byte arr[] = response.body(asByteArray());
if (!Arrays.equals(arr, buf)) {
throw new RuntimeException();
}
break;
case "file":
response.body(asFile(Paths.get("barf.txt")));
Path downloaded = Paths.get("barf.txt");
if (Files.size(downloaded) != fileContents.length()) {
throw new RuntimeException("Size mismatch");
}
break;
case "InputStream":
InputStream is = response.body(asInputStream());
byte arr1[] = new byte[1024];
int byteRead;
StringBuilder sb = new StringBuilder();
while ((byteRead = is.read(arr1)) != -1) {
sb.append(new String(arr1, 0, byteRead));
}
if (!sb.toString().equals(fileContents)) {
throw new RuntimeException();
}
break;
}
}