Java 11 新特性HttpClient

2018-09-29  本文已影响237人  redexpress

这里是Java 11 新特性HTTP Client (Standard)的简单演示:

package io.github.redexpress;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpClient.Version;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.nio.file.Paths;
import java.time.Duration;

public class HelloJava11 {
    public static void main(String[] a) throws Exception{
        new HelloJava11().demoHttp();
    }
    
    public void demoHttp() throws Exception{
        httpSync();
        httpAsync();
        postFile();
        postMultipart();
    }
    
    public void postFile() throws FileNotFoundException {
        HttpRequest request = HttpRequest.newBuilder()
                .version(Version.HTTP_1_1)
                .uri(URI.create("http://httpbin.org/post"))
                .timeout(Duration.ofMinutes(2))
                .header("Content-Type", "octet-stream")
                .POST(BodyPublishers.ofFile(Paths.get("HelloJava11.java")))
                .build();
        HttpClient client = HttpClient.newBuilder().build();
        client.sendAsync(request, BodyHandlers.ofString())
                .thenApply(HttpResponse::body)
                .thenAccept(System.out::println)
                .join();
    }
    
    public void postMultipart() throws FileNotFoundException {
        String boundary = "B-0-u-n-d-a-r-y";
        String data = "Hello Yang!";
        HttpRequest request = HttpRequest.newBuilder()
                .version(Version.HTTP_1_1)
                .uri(URI.create("http://httpbin.org/post"))
                .timeout(Duration.ofMinutes(2))
                .header("Content-Type", "multipart/form-data, boundary=" + boundary)
                .POST(BodyPublishers.ofString("--" + boundary + "\r\n" 
                        + "content-disposition: form-data; filename=\"text\"\r\n"
                        + "Content-Type: text/plain\r\n" 
                        + "\r\n"
                        + data + "\r\n" 
                        + "--" + boundary + "--\r\n"))
                .build();
        HttpClient client = HttpClient.newBuilder().build();
        client.sendAsync(request, BodyHandlers.ofString())
                .thenApply(HttpResponse::body)
                .thenAccept(System.out::println)
                .join();
    }
    
    public void httpSync() throws IOException, InterruptedException{
        HttpRequest request = HttpRequest.newBuilder()
                .version(Version.HTTP_1_1)
                .uri(URI.create("http://httpbin.org/get"))
                .GET().build();
        HttpClient client = HttpClient.newBuilder().build();
        HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
        System.out.println(response.statusCode());
        System.out.println(response.body());
    }
    
    public void httpAsync(){
        HttpRequest request = HttpRequest.newBuilder()
                .version(Version.HTTP_1_1)
                .uri(URI.create("http://httpbin.org/get"))
                .GET().build();
        HttpClient client = HttpClient.newBuilder().build();
        client.sendAsync(request, BodyHandlers.ofString())
                .thenApply(response -> {
                    System.out.println(response.statusCode());
                    return response; 
                }).thenApply(HttpResponse::body)
                .thenAccept(System.out::println)
                .join();
    }
}

使用Java 11的Launch Single-File Source-Code Programs
特性运行这段代码:

java HelloJava11.java
上一篇 下一篇

猜你喜欢

热点阅读