Interface HttpClient.Builder
- Enclosing interface:
HttpClient
-
Method Summary
Modifier and TypeMethodDescriptionaddCertificates(byte[] certificateBytes) Adds CA certificate bytes to the client's trust store.addCertificates(Path path) Adds a CA certificate file to the client's trust store.addRewrite(URI sourcePrefix, URI targetPrefix) Adds a rewrite rule.build()Creates a newHttpClientfrom the current state of this builder.Returns anHTTPClientwrapper that defers building the actual HTTP client until the wrapper'sHttpClient.send(java.net.http.HttpRequest, java.net.http.HttpResponse.BodyHandler<T>)method is called.setConnectTimeout(Duration timeout) Sets the timeout for connecting to a server.Configures HTTP connections to connect to the provided proxy address.setProxySelector(ProxySelector proxySelector) Sets the proxy selector to use when establishing connections.setRequestTimeout(Duration timeout) Sets the timeout for the interval between sending a request and receiving response headers.setRewrites(Map<URI, URI> rewrites) Removes any existing rewrites, then adds the given rewrites.setTestPort(int port) Sets a test server's listening port.setUserAgent(String userAgent) Sets theUser-Agentheader.
-
Method Details
-
setUserAgent
Sets theUser-Agentheader.Defaults to
"Pkl/$version ($os; $flavor)". -
setConnectTimeout
Sets the timeout for connecting to a server.Defaults to 60 seconds.
-
setRequestTimeout
Sets the timeout for the interval between sending a request and receiving response headers.Defaults to 60 seconds. To set a timeout for a specific request, use
HttpRequest.Builder.timeout(java.time.Duration). -
addCertificates
Adds a CA certificate file to the client's trust store.The given file must contain X.509 certificates in PEM format.
If no CA certificates are added via this method nor
addCertificates(byte[]), the built-in CA certificates of the Pkl native executable or JVM are used. -
addCertificates
Adds CA certificate bytes to the client's trust store.The given cert must be an X.509 certificate in PEM format.
If no CA certificates are added via this method nor
addCertificates(Path), the built-in CA certificates of the Pkl native executable or JVM are used. -
setTestPort
Sets a test server's listening port.If set, requests that specify port 0 will be modified to use the given port. This is an internal test option.
-
setProxySelector
Sets the proxy selector to use when establishing connections.Defaults to:
ProxySelector.getDefault(). -
setProxy
Configures HTTP connections to connect to the provided proxy address.The provided
proxyAddressmust have scheme http, not contain userInfo, and not have a path segment.If
proxyAddressisnull, uses the proxy address provided byProxySelector.getDefault().NOTE: Due to a limitation in the JDK, this does not configure the proxy server used for certificate revocation checking. To configure the certificate revocation checker, the result of
ProxySelector.getDefault()needs to be changed either by setting system properties, or viaProxySelector.setDefault(java.net.ProxySelector).- Throws:
IllegalArgumentException- if `proxyAddress` is invalid.
-
setRewrites
Removes any existing rewrites, then adds the given rewrites.A rewrite changes outbound HTTP URLs by replacing a source prefix with a targert prefix.
Each rewrite URI must start with
http://orhttps://, and end with/.Each key describes the prefix of a request, and each value describes the replacement prefix.
This can be useful for setting up mirroring of packages, which are fetched over HTTPS.
In the case of multiple matches, the longest prefix is used.
The URL hostname is case-insensitive.
- Throws:
IllegalArgumentException- ifrewritesis invalid.- Since:
- 0.29.0
-
addRewrite
Adds a rewrite rule.- Throws:
IllegalArgumentException- ifsourcePrefixortargetPrefixis invalid.- Since:
- 0.29.0
- See Also:
-
build
HttpClient build()Creates a newHttpClientfrom the current state of this builder.- Throws:
HttpClientInitException- if an error occurs while initializing the client
-
buildLazily
HttpClient buildLazily()Returns anHTTPClientwrapper that defers building the actual HTTP client until the wrapper'sHttpClient.send(java.net.http.HttpRequest, java.net.http.HttpResponse.BodyHandler<T>)method is called.Note: When using this method, any exception thrown when building the actual HTTP client is equally deferred.
-