Java Code Examples for java.net.http.HttpResponse

The following are top voted examples for showing how to use java.net.http.HttpResponse. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: openjdk9   File: Timeout.java   Source Code and License 6 votes vote down vote up
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);
    }
}
 
Example 2
Project: openjdk9   File: RequestBodyTest.java   Source Code and License 6 votes vote down vote up
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");
}
 
Example 3
Project: openjdk9   File: TLSConnection.java   Source Code and License 5 votes vote down vote up
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);
}
 
Example 4
Project: openjdk9   File: SplitResponse.java   Source Code and License 5 votes vote down vote up
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");
}
 
Example 5
Project: Java-9-Jigsaw-Example   File: TimeNetworkProvider.java   Source Code and License 5 votes vote down vote up
@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");
    }
}
 
Example 6
Project: openjdk9   File: HttpUtils.java   Source Code and License 4 votes vote down vote up
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;
    }
}