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

Java 11 HttpClient - What is Optimum Ratio of HttpClients to Concurrent HttpRequests

In the example below I create one Java 11 httpClient and then create multiple concurrent HttpRequests. Is this bad practice? Should each HttpRequest have its own HttpClient? Is there an upper limit on the number of HttpRequests a HttpClient can…
Adrian Smith
  • 1,013
  • 1
  • 13
  • 21
5
votes
1 answer

"java.io.IOException: Connection timed out" VS HttpTimeoutException On java 11 HTTP Client

I'm using Java 11 http-client (java.net.http). The send() method declares these exceptions: @throws IOException @throws InterruptedException @throws IllegalArgumentException @throws SecurityException I am interested in catching exceptions caused by…
user12396421
  • 175
  • 1
  • 10
5
votes
3 answers

Is there a way to fetch the Reason-Phrase from the status line of a HTTP 1.1 response with Java 11's HttpClient?

With Java's java.net.HttpURLConnection there is a getResponseMessage() method to retrieve any Reason-Phrase text (as specified by RFC2616 6.1 Status Line). In the newer Java 11 HttpClient, java.net.http.HttpResponse only has a statusCode()…
Nathan Williams
  • 951
  • 9
  • 12
5
votes
4 answers

SSE Java 11+ client example (w/o dependencies)

I'm looking for examples using a plain JDK11+ http client reading server sent events, without extra dependencies. I can't find anything about sse in the documentation either. Any hints?
bart van deenen
  • 661
  • 6
  • 16
5
votes
0 answers

Java 11 Connection Reset (Intermitent)

I'm using Java11 and httpclient 4.5.11 to make request to APIs, and so far everything works. But more than a week ago payments to https://api.moip.com.br started giving intermitent errors (Connection reset). I know that they require TLSv1.2 and…
Lucas Basquerotto
  • 7,260
  • 2
  • 47
  • 61
5
votes
3 answers

Cancellation of http request in Java 11 HttpClient

I'm trying to cancel http request via new Java 11 HttpClient. This is my test code: import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; public class App { public static…
5
votes
2 answers

JAVA-11: java.net.httpClient How to set Retry?

Currently I'm replacing existing org.apache.http.* http client library with JDK-11's new Http library. There are many exciting new features, however I've not found anything on "how to set RetryRequestHandler in new HTTPClient". Code snippet of…
MD Ruhul Amin
  • 4,386
  • 1
  • 22
  • 37
5
votes
2 answers

Wrapping BodySubscriber in GZIPInputStream leads to hang

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 so…
Bobulous
  • 12,967
  • 4
  • 37
  • 68
4
votes
1 answer

How to add parameters to java.net.http.HttpClient GET request?

We found the following example, which works: import java.net.http.HttpClient; : private static final HttpClient httpClient = HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1) …
John Little
  • 10,707
  • 19
  • 86
  • 158
4
votes
0 answers

https proxy with JDK11 client

I'd like to use the JDK 11 HttpClient to perform a request through an HTTPS proxy. I've tried this code to create the client: HttpClient client = HttpClient .newBuilder() .proxy(ProxySelector.of(new InetSocketAddress("my.proxy.host",8443))) …
Yonatan
  • 2,543
  • 2
  • 19
  • 20
4
votes
2 answers

How to do Preemptive authentication using Java 11 HTTP client?

I am trying to use Java 11 HTTP Client against an authenticated service, using Basic Authentication. The authentication occurs successfully but it makes an additional round-trip to the server, to understand it should send the authentication…
fsou
  • 78
  • 1
  • 6
4
votes
2 answers

How can I send a request using the HTTP Options method with HTTPClient (Java 11)?

As the title says, I would like to know how to send an HTTP request using HTTPClient with the OPTIONS method in Java. I have looked at the HTTP methods that can be used (GET, POST, PUT and DELETE) and I have not seen this option. An example of a…
PablooD9
  • 83
  • 7
4
votes
1 answer

Is there a way to use java.net.http.HttpClient with Oauth2?

Hello I switched from RestTemplate to HttpClient from java 11. I want to switch from OAuth2RestTemplate to HttpClient from java 11 also. I cannot find any materials about using HttpClient with OAuth2. Is it possible in easy way? And is it good…
4
votes
1 answer

How to handle errors when executing an async request with HTTP Java Client?

I'm using the new Java 11 HTTP Client. I have a request like: httpClient.sendAsync(request, discarding()) How to add a handler for HTTP errors? I need to log errors but I want to keep the request async. Currently, HTTP errors like 400 or 500, are…
Luís Soares
  • 5,726
  • 4
  • 39
  • 66
4
votes
1 answer

JDK 11 HttpClient throws "No subject alternative DNS name" error

I am writing a simple REST api client based on JDK11 HttpClient, simple code as below: public class MyClass { private static final X509TrustManager TRUST_MANAGER = new X509TrustManager() { public void…
Andrey
  • 41
  • 1
  • 2