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
2
votes
0 answers

java.net.http.HttpClient Error Handling with different BodySubscribers

Currently, I have this JsonBodyHandler used to parse the response body if the response is successful. I also have a model defined if the request is not successful. So based on the status code, I need to map the response to either the expected or…
Keshavram Kuduwa
  • 942
  • 10
  • 40
2
votes
1 answer

How to execute "OPTIONS" HTTP method request in Java 11 HttpClient

I need to replace Apache http client with java httpClient. I have a test with an apache client that works well: HttpOptions httpOptions = new HttpOptions(uploadUrl); try (CloseableHttpResponse responseOld = httpclient.execute(httpOptions)) { …
Aleks Crow
  • 51
  • 4
2
votes
2 answers

How to convert HttpResponse to Class in Java with HttpClient?

I am using the Java HttpClient (java.net.http) and I am sending a GET request. The response I get is a JSON String of the class "UserDto". HttpResponse send = httpClient.send(accept, HttpResponse.BodyHandlers.ofString()); I don't want to…
LeTest
  • 73
  • 7
2
votes
1 answer

How can I prevent java.net.http.HttpClient from making an upgrade request?

Upon making a call from a Java project to a Python rest API, I am met with an error that states "Unsupported upgrade request." I'm making a simple GET request from Java, to the endpoint written in Python, and at some point Java decides it wants to…
Kristoff
  • 167
  • 3
  • 13
2
votes
1 answer

Reloading a java.net.http.HttpClient's SSLContext

I've got a program that makes use of the java.net.http.HttpClient, which was introduced in Java 11, to connect and send requests to internal services. These services are mutually authenticated, both presenting certificates issued by an internal…
Savior
  • 3,225
  • 4
  • 24
  • 48
2
votes
1 answer

How to write a request body using a stream with the java 11 http client?

The legacy java http client provided an OutputStream using URLConnection.getOutputStream. Writing a json body for example worked like the following: final URLConnection urlConnection = ...; try (OutputStreamWriter writer = new…
Florian
  • 23
  • 1
  • 5
2
votes
1 answer

How to send a PUT request with multipart/form-data body with Java Http Client?

I am using the HttpClient and HttpRequest available on java.net.http. I have successfully sent some GET and POST requests, but I have no clue about how to send a PUT/POST request with multipart/form-data body: HttpRequest dataRequest =…
moictab
  • 959
  • 6
  • 27
2
votes
2 answers

Is there a way to put a CompletableFuture in a loop?

The problem with the code below is that I have to wait for all three tasks to finish. If the 1st and 2nd tasks complete in 200ms and the 3rd completes in 2s then I will have to wait 2s before I load the next three URLs. Ideally I would send a new…
Adrian Smith
  • 1,013
  • 1
  • 13
  • 21
2
votes
1 answer

How to consume an InputStream in an HttpResponse.BodyHandler?

I'm trying to build a JSON handler for the Java 11 HttpClient. The problem I'm having is that trying to compose with BodySubscribers.ofInputStream() results in never reading any data from the stream, and hanging forever. @Override public…
OrangeDog
  • 36,653
  • 12
  • 122
  • 207
2
votes
0 answers

Java 11 HttpClient Hangs Intermittently

I am using Java 11's HttpClient. The client hangs intermittently and I can't figure out why -- I don't know what's causing the hang and I don't know how to reproduce it. The code to construct the client and requests are in separate locations, so…
MonkeyWithDarts
  • 755
  • 1
  • 7
  • 17
2
votes
1 answer

Unreliable response from java.net.http.HttpClient call using Java 11 and Spring Boot

I'm writing a back-end application in Spring Boot that calls another API from a third party. I'm having a problem with this particular call, which retrieves a token object containing a bearer token, which then I use in their other endpoints. The…
2
votes
4 answers

How to disable openJdk 11 java.net.http.HttpClient logging?

I'm using httpclient from openJDK 11 that comes with an annoying recurrent logging. Here how it looks like (I'm using spring boot based app): 2020-01-24 18:35:46,806 DEBUG [HttpClient-1-SelectorManager] : [HttpClient-1-SelectorManager] [3840s 992ms]…
user12776952
2
votes
2 answers

How to use Proxy password authentication on Java's Native HTTP Client

I'm trying to use password authentication if a proxy requires it for Java's Native HTTP Client requests. I have tried a bunch of stuff, with the most common solution applied to my program being String username = "username"; String password =…
2
votes
0 answers

Persistent connections (keep alive) with Java HttpClient

We are building an application using the new (well comparatively) Java HttpClient that was added in Java 11. We are on Java 12. Overall the API is nice, but I can't find anything related to persistent connections. Most other similar APIs support…
ewramner
  • 5,810
  • 2
  • 17
  • 33
2
votes
0 answers

java.net.HttpClient fails with EOFException while making POST

I have an application that uses openjdk-11.0.1 java.net.HttpClient to make a multipart POST request to a REST(ish) endpoint. That endpoint blocks the response while it does whatever work, and when the work is done, it returns the response. The…
liltitus27
  • 1,670
  • 5
  • 29
  • 46