Java 类java.net.http.HttpClient 实例源码

项目:openjdk9    文件:ManyRequests.java   
public static void main(String[] args) throws Exception {
    Logger logger = Logger.getLogger("com.sun.net.httpserver");
    logger.setLevel(Level.ALL);
    logger.info("TEST");

    SSLContext ctx = new SimpleSSLContext().get();

    InetSocketAddress addr = new InetSocketAddress(0);
    HttpsServer server = HttpsServer.create(addr, 0);
    server.setHttpsConfigurator(new Configurator(ctx));

    HttpClient client = HttpClient.create()
                                  .sslContext(ctx)
                                  .build();
    try {
        test(server, client);
        System.out.println("OK");
    } finally {
        server.stop(0);
        client.executorService().shutdownNow();
    }
}
项目:openjdk9    文件:Timeout.java   
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);
    }
}
项目:openjdk9    文件:TimeoutTest.java   
public static void main(String[] args) throws Exception {
    try {
        dotest();
    } finally {
        HttpClient.getDefault().executorService().shutdownNow();
        executor.shutdownNow();
    }
}
项目:openjdk9    文件:TLSConnection.java   
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);
}
项目:openjdk9    文件:SplitResponse.java   
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");
}
项目:Java-9-Jigsaw-Example    文件:TimeNetworkProvider.java   
@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");
    }
}
项目:openjdk9    文件:HttpUtils.java   
public static HttpRequest.Builder getHttpRequestBuilder(final HttpClient client,
                                                        final String requestType,
                                                        final URI uri)
    throws IOException
{
    HttpRequest.Builder builder;
    String filename = smallFile.toFile().getAbsolutePath();
    String fileContents = HttpUtils.getFileContent(filename);
    byte buf[] = fileContents.getBytes();
    switch (requestType) {
        case "InputStream":
            InputStream inputStream = new FileInputStream(smallFile.toFile());
            builder = client.request(uri)
                            .body(fromInputStream(inputStream));
            break;
        case "byteArray":
            builder = client.request(uri)
                            .body(fromByteArray(buf));
            break;
        case "byteArrays":
            Iterable iterable = Arrays.asList(buf);
            builder = client.request(uri)
                            .body(fromByteArrays(iterable.iterator()));
            break;
        case "string":
            builder = client.request(uri)
                            .body(fromString(fileContents));
            break;
        case "byteArray_offset":
            builder = client.request(uri)
                            .body(fromByteArray(buf,
                                                DEFAULT_OFFSET,
                                                DEFAULT_LENGTH));
            break;
        case "file":
            builder = client.request(uri)
                            .body(fromFile(smallFile));
            break;
        case "string_charset":
            builder = client.request(uri)
                            .body(fromString(new String(buf),
                                             Charset.defaultCharset()));
            break;
        default:
            builder = null;
            break;
    }
    return builder;
}
项目:openjdk9    文件:RequestBodyTest.java   
public static void main(String args[]) throws Exception {
    fileroot = System.getProperty("test.src") + "/docs";
    midSizedFile = Paths.get(fileroot + midSizedFilename);
    smallFile = Paths.get(fileroot + smallFilename);
    //start the server
    LightWeightHttpServer.initServer();

    httproot = LightWeightHttpServer.httproot;
    httpsroot = LightWeightHttpServer.httpsroot;
    ctx = LightWeightHttpServer.ctx;
    client = HttpClient.create().sslContext(ctx)
            .followRedirects(HttpClient.Redirect.ALWAYS)
            .executorService(Executors.newCachedThreadPool())
            .build();

    String TARGET = httproot + "echo/foo";
    boolean isSync = false;
    requestBodyTypes(TARGET, STRING, STRING, isSync);
    requestBodyTypes(TARGET, STRING, BYTE_ARRAY, isSync);
    requestBodyTypes(TARGET, STRING, BYTE_ARRAYS, isSync);
    requestBodyTypes(TARGET, STRING, INPUTSTREAM, isSync);
    requestBodyTypes(TARGET, STRING, FILE, isSync);

    requestBodyTypes(TARGET, BYTE_ARRAY, STRING, isSync);
    requestBodyTypes(TARGET, BYTE_ARRAY, BYTE_ARRAY, isSync);
    requestBodyTypes(TARGET, BYTE_ARRAY, BYTE_ARRAYS, isSync);
    requestBodyTypes(TARGET, BYTE_ARRAY, INPUTSTREAM, isSync);
    requestBodyTypes(TARGET, BYTE_ARRAY, FILE, isSync);

    requestBodyTypes(TARGET, BYTE_ARRAYS, STRING, isSync);
    requestBodyTypes(TARGET, BYTE_ARRAYS, BYTE_ARRAY, isSync);
    requestBodyTypes(TARGET, BYTE_ARRAYS, BYTE_ARRAYS, isSync);
    requestBodyTypes(TARGET, BYTE_ARRAYS, INPUTSTREAM, isSync);
    requestBodyTypes(TARGET, BYTE_ARRAYS, FILE, isSync);

    requestBodyTypes(TARGET, INPUTSTREAM, STRING, isSync);
    requestBodyTypes(TARGET, INPUTSTREAM, BYTE_ARRAY, isSync);
    requestBodyTypes(TARGET, INPUTSTREAM, BYTE_ARRAYS, isSync);
    requestBodyTypes(TARGET, INPUTSTREAM, INPUTSTREAM, isSync);
    requestBodyTypes(TARGET, INPUTSTREAM, FILE, isSync);

    requestBodyTypes(TARGET, FILE, STRING, isSync);
    requestBodyTypes(TARGET, FILE, BYTE_ARRAY, isSync);
    requestBodyTypes(TARGET, FILE, BYTE_ARRAYS, isSync);
    requestBodyTypes(TARGET, FILE, INPUTSTREAM, isSync);
    requestBodyTypes(TARGET, FILE, FILE, isSync);

    isSync = true;
    requestBodyTypes(TARGET, STRING, STRING, isSync);
    requestBodyTypes(TARGET, STRING, BYTE_ARRAY, isSync);
    requestBodyTypes(TARGET, STRING, BYTE_ARRAYS, isSync);
    requestBodyTypes(TARGET, STRING, INPUTSTREAM, isSync);
    requestBodyTypes(TARGET, STRING, FILE, isSync);

    requestBodyTypes(TARGET, BYTE_ARRAY, STRING, isSync);
    requestBodyTypes(TARGET, BYTE_ARRAY, BYTE_ARRAY, isSync);
    requestBodyTypes(TARGET, BYTE_ARRAY, BYTE_ARRAYS, isSync);
    requestBodyTypes(TARGET, BYTE_ARRAY, INPUTSTREAM, isSync);
    requestBodyTypes(TARGET, BYTE_ARRAY, FILE, isSync);

    requestBodyTypes(TARGET, BYTE_ARRAYS, STRING, isSync);
    requestBodyTypes(TARGET, BYTE_ARRAYS, BYTE_ARRAY, isSync);
    requestBodyTypes(TARGET, BYTE_ARRAYS, BYTE_ARRAYS, isSync);
    requestBodyTypes(TARGET, BYTE_ARRAYS, INPUTSTREAM, isSync);
    requestBodyTypes(TARGET, BYTE_ARRAYS, FILE, isSync);

    requestBodyTypes(TARGET, INPUTSTREAM, STRING, isSync);
    requestBodyTypes(TARGET, INPUTSTREAM, BYTE_ARRAY, isSync);
    requestBodyTypes(TARGET, INPUTSTREAM, BYTE_ARRAYS, isSync);
    requestBodyTypes(TARGET, INPUTSTREAM, INPUTSTREAM, isSync);
    requestBodyTypes(TARGET, INPUTSTREAM, FILE, isSync);

    requestBodyTypes(TARGET, FILE, STRING, isSync);
    requestBodyTypes(TARGET, FILE, BYTE_ARRAY, isSync);
    requestBodyTypes(TARGET, FILE, BYTE_ARRAYS, isSync);
    requestBodyTypes(TARGET, FILE, INPUTSTREAM, isSync);
    requestBodyTypes(TARGET, FILE, FILE, isSync);

}