java.net.http.HttpClient.Version Java Examples

The following examples show how to use java.net.http.HttpClient.Version. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HttpClientExample.java    From tutorials with MIT License 6 votes vote down vote up
public static void pushRequest() throws URISyntaxException, InterruptedException {
    System.out.println("Running HTTP/2 Server Push example...");

    HttpClient httpClient = HttpClient.newBuilder()
        .version(Version.HTTP_2)
        .build();

    HttpRequest pageRequest = HttpRequest.newBuilder()
        .uri(URI.create("https://http2.golang.org/serverpush"))
        .build();

    // Interface HttpResponse.PushPromiseHandler<T>
    // void applyPushPromise​(HttpRequest initiatingRequest, HttpRequest pushPromiseRequest, Function<HttpResponse.BodyHandler<T>,​CompletableFuture<HttpResponse<T>>> acceptor)
    httpClient.sendAsync(pageRequest, BodyHandlers.ofString(), pushPromiseHandler())
        .thenAccept(pageResponse -> {
            System.out.println("Page response status code: " + pageResponse.statusCode());
            System.out.println("Page response headers: " + pageResponse.headers());
            String responseBody = pageResponse.body();
            System.out.println(responseBody);
        }).join();

    Thread.sleep(1000); // waiting for full response
}
 
Example #2
Source File: JavaRequestProcessor.java    From milkman with MIT License 5 votes vote down vote up
@SneakyThrows
	private HttpClient buildClient() {
		Builder builder = HttpClient.newBuilder();
		if (!HttpOptionsPluginProvider.options().isHttp2Support()){
			builder.version(Version.HTTP_1_1);
		}

		if (HttpOptionsPluginProvider.options().isUseProxy()) {
			URL url = new URL(HttpOptionsPluginProvider.options().getProxyUrl());
			builder.proxy(new ProxyExclusionRoutePlanner(url, HttpOptionsPluginProvider.options().getProxyExclusion()).java());
			
			//we dont use Authenticator because it might result in an exception if there is a 401 response
			// see https://github.com/AdoptOpenJDK/openjdk-jdk11/blob/master/src/java.net.http/share/classes/jdk/internal/net/http/AuthenticationFilter.java#L263
			//we manually add Proxy-Authorization header instead
//			if (proxyCredentials != null) {
//				builder.authenticator(new Authenticator() {
//					@Override
//					protected PasswordAuthentication getPasswordAuthentication() {
//						return proxyCredentials;
//					}
//				});
//			}
		}

		if (!HttpOptionsPluginProvider.options().isCertificateValidation()) {
			disableSsl(builder);
		}
		
		if (HttpOptionsPluginProvider.options().isFollowRedirects()) {
			builder.followRedirects(Redirect.ALWAYS);
		}

		setupSslLegacyProtocolSupport(builder);

		return builder
				.build();
	}
 
Example #3
Source File: JavaRequestProcessor.java    From milkman with MIT License 5 votes vote down vote up
private void buildStatusView(ResponseInfo httpResponse, RestResponseContainer response, long responseTimeInMs) {
	String versionStr = httpResponse.version().toString();
	if (httpResponse.version() == Version.HTTP_1_1) {
		versionStr = "1.1";
	} else if (httpResponse.version() == Version.HTTP_2) {
		versionStr = "2.0";
	}
	LinkedHashMap<String, String> statusKeys = new LinkedHashMap<>();
	statusKeys.put("Status", ""+httpResponse.statusCode());
	statusKeys.put("Time", responseTimeInMs + "ms");
	statusKeys.put("Http", versionStr);
	
	response.getStatusInformations().complete(statusKeys);
}
 
Example #4
Source File: OreKitDataClient.java    From r2cloud with Apache License 2.0 5 votes vote down vote up
public OreKitDataClient(List<String> urls) {
	if (urls == null || urls.isEmpty()) {
		throw new IllegalArgumentException("urls are blank. at least 1 is expected");
	}
	this.urls = urls;
	this.httpclient = HttpClient.newBuilder().version(Version.HTTP_2).followRedirects(Redirect.NORMAL).connectTimeout(Duration.ofMillis(TIMEOUT)).build();
}
 
Example #5
Source File: RestClient.java    From r2cloud with Apache License 2.0 5 votes vote down vote up
public RestClient(String baseUrl) throws Exception {
	if (baseUrl == null) {
		this.baseUrl = "http://localhost:8097";
	} else {
		this.baseUrl = baseUrl;
	}
	this.httpclient = HttpClient.newBuilder().version(Version.HTTP_2).followRedirects(Redirect.NORMAL).connectTimeout(Duration.ofMinutes(1L)).build();
}
 
Example #6
Source File: HttpClientExample.java    From tutorials with MIT License 5 votes vote down vote up
public static void httpGetRequest() throws URISyntaxException, IOException, InterruptedException {
    HttpClient client = HttpClient.newHttpClient();
    HttpRequest request = HttpRequest.newBuilder()
        .version(HttpClient.Version.HTTP_2)
        .uri(URI.create("http://jsonplaceholder.typicode.com/posts/1"))
        .headers("Accept-Enconding", "gzip, deflate")
        .build();
    HttpResponse<String> response = client.send(request, BodyHandlers.ofString());

    String responseBody = response.body();
    int responseStatusCode = response.statusCode();

    System.out.println("httpGetRequest: " + responseBody);
    System.out.println("httpGetRequest status code: " + responseStatusCode);
}
 
Example #7
Source File: HttpClientExample.java    From tutorials with MIT License 5 votes vote down vote up
public static void httpPostRequest() throws URISyntaxException, IOException, InterruptedException {
    HttpClient client = HttpClient.newBuilder()
        .version(HttpClient.Version.HTTP_2)
        .build();
    HttpRequest request = HttpRequest.newBuilder(new URI("http://jsonplaceholder.typicode.com/posts"))
        .version(HttpClient.Version.HTTP_2)
        .POST(BodyPublishers.ofString("Sample Post Request"))
        .build();
    HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
    String responseBody = response.body();
    System.out.println("httpPostRequest : " + responseBody);
}
 
Example #8
Source File: HttpClientExample.java    From tutorials with MIT License 5 votes vote down vote up
public static void asynchronousGetRequest() throws URISyntaxException {
    HttpClient client = HttpClient.newHttpClient();
    URI httpURI = new URI("http://jsonplaceholder.typicode.com/posts/1");
    HttpRequest request = HttpRequest.newBuilder(httpURI)
        .version(HttpClient.Version.HTTP_2)
        .build();
    CompletableFuture<Void> futureResponse = client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
        .thenAccept(resp -> {
            System.out.println("Got pushed response " + resp.uri());
            System.out.println("Response statuscode: " + resp.statusCode());
            System.out.println("Response body: " + resp.body());
        });
    System.out.println("futureResponse" + futureResponse);

}
 
Example #9
Source File: JavaRequestProcessor.java    From milkman with MIT License 4 votes vote down vote up
@Override
public Version version() {
	return null;
}
 
Example #10
Source File: R2ServerClient.java    From r2cloud with Apache License 2.0 4 votes vote down vote up
public R2ServerClient(Configuration config) {
	this.config = config;
	this.hostname = config.getProperty("r2server.hostname");
	this.httpclient = HttpClient.newBuilder().version(Version.HTTP_2).followRedirects(Redirect.NORMAL).connectTimeout(Duration.ofMillis(config.getInteger("r2server.connectionTimeout"))).build();
}
 
Example #11
Source File: NoIpClient.java    From r2cloud with Apache License 2.0 4 votes vote down vote up
public NoIpClient(String hostname, String username, String password) {
	this.hostname = hostname;
	this.username = username;
	this.password = password;
	this.httpclient = HttpClient.newBuilder().version(Version.HTTP_2).followRedirects(Redirect.NORMAL).connectTimeout(Duration.ofMinutes(1L)).build();
}
 
Example #12
Source File: ExternalIpClient.java    From r2cloud with Apache License 2.0 4 votes vote down vote up
public ExternalIpClient(String host) {
	this.host = host;
	this.httpclient = HttpClient.newBuilder().version(Version.HTTP_2).followRedirects(Redirect.NORMAL).connectTimeout(Duration.ofMinutes(1L)).build();
}
 
Example #13
Source File: Http2Client.java    From feign with Apache License 2.0 4 votes vote down vote up
public Http2Client() {
  this(HttpClient.newBuilder()
      .followRedirects(Redirect.ALWAYS)
      .version(Version.HTTP_2)
      .build());
}