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
52
votes
10 answers

How to upload a file using Java HttpClient library working with PHP

I want to write Java application that will upload a file to the Apache server with PHP. The Java code uses Jakarta HttpClient library version 4.0 beta2: import java.io.File; import org.apache.http.HttpEntity; import…
Piotr Kochański
  • 21,862
  • 7
  • 70
  • 77
36
votes
3 answers

Allow insecure HTTPS connection for Java JDK 11 HttpClient

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…
vagran
  • 867
  • 1
  • 7
  • 18
35
votes
3 answers

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

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…
Abhijit Sarkar
  • 21,927
  • 20
  • 110
  • 219
34
votes
5 answers

Java 11: New HTTP client send POST requests with x-www-form-urlencoded parameters

I'm trying to send a POST request using the new http client api. Is there a built in way to send parameters formatted as x-www-form-urlencoded ? My current code: HttpRequest request = HttpRequest.newBuilder() .uri(URI.create(url)) …
Doua Beri
  • 10,612
  • 18
  • 89
  • 138
27
votes
7 answers

Close Java HTTP Client

Is there a way to close java.net.http.HttpClient to instantly release resources held by it? Internally it holds a selector, a connection pool and an Executor (when default one is used). However it does not implement Closeable/AutoCloseable.
Vladimir Petrakovich
  • 4,184
  • 1
  • 30
  • 46
23
votes
3 answers

Deserializing JSON using Java 11 HttpClient and custom BodyHandler with Jackson halts and will not proceed

I have a problem with deserializing JSON to custom object directly using Java 11 HttpClient::send with custom HttpResponse.BodyHandler. I came across this issue while answering this SO question. Versions that I am using : OpenJDK 11 Jackson…
Michał Krzywański
  • 15,659
  • 4
  • 36
  • 63
23
votes
1 answer

Java 11 HttpClient not sending basic authentication

I wrote the following HttpClient code, and it did not result in an Authorization header being sent to the server: public static void main(String[] args) { var client = HttpClient.newBuilder() .authenticator(new Authenticator() { …
Jonathan Fuerth
  • 2,080
  • 2
  • 18
  • 21
20
votes
3 answers

How to map a JSON response to a Java class using Java 11 HttpClient and Jackson?

I'm new to the Java 11 HttpClient and would like to give it a try. I have a simple GET request that return JSON and I would like to map the JSON response to a Java class called Questionnaire. I understand that I can turn the response out of box into…
saw303
  • 8,051
  • 7
  • 50
  • 90
17
votes
2 answers

How to set socket timeout in Java HTTP Client

We want to migrate all our apache-httpclient-4.x code to java-http-client code to reduce dependencies. While migrating them, I ran into the following issue under Java 11: How to set the socket timeout in Java HTTP Client? With apache-httpclient-4.x…
mseele
  • 256
  • 1
  • 2
  • 11
17
votes
2 answers

How to customise "host" header in Java http client

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,…
Krzysztof Krasoń
  • 26,515
  • 16
  • 89
  • 115
15
votes
1 answer

How to handle HTTP/2 GOAWAY with HttpClient?

I am trying to continuously send GET and POST requests to a REST API every few minutes. The issue is that after exactly 1000 requests I receive a GOAWAY frame (and an IOException): The GOAWAY frame (type=0x7) is used to initiate shutdown of a…
Salem
  • 13,516
  • 4
  • 51
  • 70
13
votes
2 answers

How to test java.net.http (Java 11) requests BodyPublisher?

I'm trying to test my code which uses the new Java 11 java.net.http.HttpClient. In my production code I have something like this: HttpClient httpClient = ... (gets injected) HttpRequest request =…
Itchy
  • 2,263
  • 28
  • 41
13
votes
2 answers

How to define multiple parameters for a POST request using Java 11 HTTP Client

I have a code that makes a POST request for a specific endpoint. This code is using Apache's HttpClient and I would like to start using the native HttpClient from Java (JDK11). But I didn't understand how to specify the parameters of my…
Renan Gomes
  • 771
  • 1
  • 15
  • 34
13
votes
3 answers

Require assistance with simple pure Java 11 WebSocket client example

There appears to be very little Java 11 (pure Java non framework based) WebSocket client code examples on the web so I'm hoping StackOverflow can come to the rescue for me once again. This is the closest I've found, but unfortunately to my (novice)…
eodeluga
  • 608
  • 2
  • 7
  • 20
13
votes
1 answer

Java 9 HttpClient exception when using certain characters in URL query parameters

Here is my sample code. The query is encoded to UTF-8: HttpRequest request = HttpRequest.newBuilder() .header("content-type", "application/json;charset=UTF-8") .uri(URI.create("http://localhost:8080/test?param1=test%C5%84")) .GET() …
lassa
  • 173
  • 9
1
2 3
11 12