Questions tagged [java-http-client]

This tag should be used for questions which relate specifically to the new JDK 11 `java.net.http` package which includes new classes and interfaces such as HttpClient, HttpRequest, HttpResponse, and WebSocket. This tag should not be used for questions which relate to HTTP handling with the older `java.net` package. This tag can be used for questions about the incubator versions of this package, but do make the JDK version clear in such cases.

This tag should be used for questions which relate specifically to the new Java HTTP Client and WebSocket APIs in package java.net.http which was formally introduced in JDK 11 and includes new classes and interfaces such as HttpClient, HttpRequest, HttpResponse, and WebSocket.

This tag should not be used for questions which relate to HTTP handling with the older java.net package, and it should not be used for questions about third-party HTTP libraries (such as Apache HttpClient, org.apache.http). Search the tags page for tags specific to third-party HTTP libraries.

Note that an incubator version of this new framework was included in JDK 9 and JDK 10. There are significant differences between the incubator versions and the finalised version which was released in JDK 11.

This tag can be used to ask questions about the incubator versions of this new package, but in such cases the question should clearly state the JDK version and make it clear that an incubator version of the framework is being discussed. Such questions should also be tagged with or to make it clear that an older JDK is being used.

179 questions
12
votes
1 answer

Is it possible to intercept Java 11 HttpClient requests?

Basically as the title says. Apache HttpClient and Spring RestTemplate allow for defining custom interceptors which wrap around requests/responses and allow for additional (global) modification of request parameters, logging, etc... I do not see…
MrPlow
  • 1,295
  • 3
  • 26
  • 45
12
votes
3 answers

Does Java HTTP Client handle compression

I tried to find any mention of handling of compression in new Java HTTP Client but failed. Is there a built-in configuration to handle for e.g. gzip or deflate compression? I would expect to have a BodyHandler for e.g. something like…
Krzysztof Krasoń
  • 26,515
  • 16
  • 89
  • 115
12
votes
3 answers

Java 9 no class definition exception

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…
EmberTraveller
  • 355
  • 3
  • 18
12
votes
1 answer

Java 9 HttpClient with sendAsyncMulti / multiResponseAsync

I am trying to experiment with Java 9's HttpClient. The basic example as in HttpRequest's javadoc works without problems: HttpResponse response = HttpRequest.create(new URI("http://stackoverflow.com/")) …
user140547
  • 7,750
  • 3
  • 28
  • 80
11
votes
1 answer

How to read the body of a HttpRequest in Java 11?

In a test, I'd like to look inside the body of a HttpRequest. I'd like to get the body as a string. It seems that the only way to do that, is to subscribe to the BodyPublisher but how does that work?
progonkpa
  • 3,590
  • 9
  • 31
  • 50
11
votes
2 answers

Connections leaking with state CLOSE_WAIT with HttpClient

We are using JDK11 java.net.http HTTP client to get data from an API. After we receive the response the connections remain opened in our server with TCP state CLOSE_WAIT, which means the client must close the connection. From RFC 793…
Evandro Pomatti
  • 13,341
  • 16
  • 97
  • 165
10
votes
2 answers

How to keep connection alive in Java 11 http client

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?
qiGuar
  • 1,726
  • 1
  • 18
  • 34
9
votes
2 answers

How / where to check for java.net.http.HttpClient HTTP response status codes

Java 11's java.net.http.HttpClient does not seem to check the HTTP status code, except for redirects (if enabled). And all examples found in the wiki and in the Java API documentation always assume that HTTP requests are successful, i.e. they never…
Marcono1234
  • 5,856
  • 1
  • 25
  • 43
9
votes
2 answers

Java WebSocket message limit

I'm trying to create communication between simple Java App (using java.net.http.WebSocket class) and remote google-chrome run using google-chrome --remote-debugging-port=9222 --user-data-dir=. Sending and receiving small messages works as expected,…
9
votes
1 answer

Java 11 HttpRequest with PATCH method

I try to use the java 11 HttpRequest to call the msgraph webservice using the method PATCH: import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.net.URI; import…
laloune
  • 548
  • 1
  • 9
  • 26
9
votes
0 answers

Open JDK 11 HTTP/2 Handshake ServerHello java.util.NoSuchElementException

When testing the Open JDK 11 HTTP client over HTTP/2, there is a server-side error which looks like a JDK 11 bug. The test runs several threads against a Tomcat 9 server, testing that all threads use HTTP/2 and TLS1.3. The concern is that the…
user304217
  • 153
  • 6
9
votes
1 answer

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

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"…
user1120821
  • 439
  • 7
  • 18
9
votes
3 answers

Proxy Authentication with JDK 11 HttpClient

I'm trying to use JDK 11 HttpClient to make requests through a corporate proxy which requires authentication by login and password. According to JDK's intro, I'm building an instance of client by means of: HttpClient httpClient =…
Toparvion
  • 799
  • 2
  • 9
  • 19
8
votes
2 answers

How to timeout on a slow streaming response body with Java's HttpClient

How should I deal with servers that hang sending an HTTP response body using the HTTP client included in Java 11 onwards, when I need to handle the response in a streaming fashion? Having read the documentation, I'm aware that it's possible to set a…
Mark Slater
  • 831
  • 7
  • 18
8
votes
2 answers

Retry HTTP request (Java 11 - HttpClient)

Problem Using HttpClient from Java 11 (JDK, not Apache), how can I retry requests? Lets say I want to retry a request up to 10 times if it did not return a status code of 200 or threw an exception. Attempt Currently I am composing the returned…
Zabuzard
  • 25,064
  • 8
  • 58
  • 82
1
2
3
11 12