java-http-client

How to keep connection alive in Java 11 http client

泄露秘密 提交于 2019-12-05 21:53:28
I'm using the Java 11 HttpClient with HTTP/2 and need to keep connection alive for few minutes, but the builder does not have the option to set it. Is there a way to specify this and make client keep the connection alive for some time? If you build a standard HttpClient e.g. using HttpClient.newHttpClient(); by default a connection pool is created. This pool keeps the connections alive by default for 1200 seconds (20 minutes). If you want to change the keep-alive timeout you can do so using the property jdk.httpclient.keepalive.timeout . However the value is only read once when the class jdk

Wrapping BodySubscriber<InputStream> in GZIPInputStream leads to hang

二次信任 提交于 2019-12-01 06:27:04
I'm using the new java.net.http classes to handle asynchronous HTTP request+response exchanges, and I'm trying to find a way to have the BodySubscriber handle different encoding types such as gzip. However, mapping a BodySubsriber<InputStream> so that the underlying stream is wrapped by a GZIPInputStream (when "Content-Encoding: gzip" is found in the response header) leads to a hang. No exceptions, just a total cessation of activity. The code which maps the BodySubscriber looks like this: private HttpResponse.BodySubscriber<InputStream> gzippedBodySubscriber( HttpResponse.ResponseInfo

In Java 11 HttpClient how to solve restricted header name: Date

﹥>﹥吖頭↗ 提交于 2019-12-01 04:31:20
The following java 11 code: HttpRequest request = HttpRequest.newBuilder() .uri(uri) .header("Digest", digest) .header("Date", date) .build(); gives the following error: Exception in thread "main" java.lang.IllegalArgumentException: restricted header name: "Date" The problem is that the digest is based on the date, so I cannot simply rely on the http client date, because that will make the digest invalid. I need a way to either set the Date header, or to retrieve the Date header and then set the digest thereafter. Neither seems possible with standard java 11. Bug# JDK-8213189 This is a bug

How to log request/response using java.net.http.HttpClient?

落花浮王杯 提交于 2019-11-30 19:32:50
The HttpClient introduced experimentally in Java 9 is now stable in Java 11, but not surprisingly, very few projects seem to actually use it. Documentation is almost non-existing. One of the most commons asks while making a HTTP call is logging of request/response. How would you do that using the HttpClient , without of course, logging it manually in every single call? Is there an interceptor mechanism like that offered by all other HTTP clients? If we look at jdk.internal.net.http.common.DebugLogger source code we can see a few loggers using System.Logger , which in turn will use System

Allow insecure HTTPS connection for Java JDK 11 HttpClient

依然范特西╮ 提交于 2019-11-30 06:57:22
Sometimes it is needed to allow insecure HTTPS connections, e.g. in some web-crawling applications which should work with any site. I used one such solution with old HttpsURLConnection API which was recently superseded by the new HttpClient API in JDK 11. What is the way to allow insecure HTTPS connections (self-signed or expired certificate) with this new API? UPD: The code I tried (in Kotlin but maps directly to Java): val trustAllCerts = arrayOf<TrustManager>(object: X509TrustManager { override fun getAcceptedIssuers(): Array<X509Certificate>? = null override fun checkClientTrusted(certs:

How to customise “host” header in Java http client

跟風遠走 提交于 2019-11-29 11:11:49
Here's my code: HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("http://127.0.0.1:8081/")) .header("Host", "test.example.com") .build(); client.send(request, HttpResponse.BodyHandlers.ofString()); As a result I see that the above code sends: GET / HTTP/1.1 Connection: Upgrade, HTTP2-Settings Content-Length: 0 Host: 127.0.0.1:8081 HTTP2-Settings: AAEAAEAAAAIAAAABAAMAAABkAAQBAAAAAAUAAEAA Upgrade: h2c User-Agent: Java-http-client/10 Host: test.example.com As you can see it sends two Host headers (the one from URI and the one I

How to keep connection alive in Java 11 http client

二次信任 提交于 2019-11-28 05:26:56
问题 I'm using the Java 11 HttpClient with HTTP/2 and need to keep connection alive for few minutes, but the builder does not have the option to set it. Is there a way to specify this and make client keep the connection alive for some time? 回答1: If you build a standard HttpClient e.g. using HttpClient.newHttpClient(); by default a connection pool is created. This pool keeps the connections alive by default for 1200 seconds (20 minutes). If you want to change the keep-alive timeout you can do so

How to customise “host” header in Java http client

寵の児 提交于 2019-11-28 03:48:17
问题 Here's my code: HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("http://127.0.0.1:8081/")) .header("Host", "test.example.com") .build(); client.send(request, HttpResponse.BodyHandlers.ofString()); As a result I see that the above code sends: GET / HTTP/1.1 Connection: Upgrade, HTTP2-Settings Content-Length: 0 Host: 127.0.0.1:8081 HTTP2-Settings: AAEAAEAAAAIAAAABAAMAAABkAAQBAAAAAAUAAEAA Upgrade: h2c User-Agent: Java-http-client/10

Java 9 no class definition exception

喜你入骨 提交于 2019-11-28 02:56:13
问题 So i want to try the http client package com.company; import jdk.incubator.http.HttpClient; public class Main { public static void main(String[] args) { HttpClient client = HttpClient.newHttpClient(); } } And my module info looks like this module com.company { requires jdk.incubator.httpclient; } But i get java.lang.NoClassDefFoundError: jdk/incubator/http/HttpClient And I don't really understand why. My java version is "build 9-ea+ 169" and I use the latest version of IntelliJ idea (2017.1.3

Java 9 HttpClient java.lang.NoClassDefFoundError: jdk/incubator/http/HttpClient

冷暖自知 提交于 2019-11-28 00:21:27
问题 I am trying to use the HttpClient from incubator in Java 9 maven project. I am not getting any Compilation issue. The project builds successfully. But when I try to run the Main class, it gives me the following exception: Exception in thread "main" java.lang.NoClassDefFoundError: jdk/incubator/http/HttpClient at java9.http_client.Main.main(Main.java:18) Caused by: java.lang.ClassNotFoundException: jdk.incubator.http.HttpClient at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass