HttpClient API usage feedback/questions

Jaikiran Pai jai.forums2013 at gmail.com
Sat Jun 2 06:22:22 UTC 2018


Hello,

I've been experimenting with the new HttpClient API that's part of Java 
now. I am using the latest EA build 16 of Java 11 from here[1]. I'm 
still in the early stages of integrating this into one of the libraries 
where we use HTTP clients.

Based on my usage so far, I have some minor 
feedback/questions/enhancement requests that I would like input on:

1. The java.net.http.HttpHeaders has firstValue and firstValueAsLong 
APIs which return an empty Optional if those headers are absent. Would 
it be possible to introduce a couple of additional APIs of the form:

String firstValueOrDefault(String name, String defaultValue) // return 
the actual header value if present, else return the passed default value

long firstValueAsLongOrDefault(String name, long defaultValue) // return 
the actual header value, as long, if present, else return the passed 
default value

Some of the places where we parse the (response) headers, we check for 
the presence of some headers and if not present then we default them to 
certain values (based on certain application specific context). In its 
current form of the HttpHeaders API, we would either have to write some 
kind of wrapper API which returns a default value for those headers or 
keep repeating the logic of checking the returned Optional for empty 
value, before defaulting it to something.

2. Would it be a good idea to add APIs (perhaps in HttpHeaders class) to 
return values of higher level constructs represented by the headers. 
Imagine certain well-known headers in the response, like Content-Length 
and Content-Type. Would it be a good idea to expose higher level APIs of 
the form getContentLength(), getContentCharset() and such? It probably 
doesn't add much value to Content-Length header but there's a particular 
mechanism the charset is supposed to be parsed out of the "Content-Type" 
header and having it done with the API's implementation would prevent 
all the parsing/logic in every other application.

3. Is the latest API javadoc this one[2]? Based on what I read and 
experimented so far, I don't see a way to set a value for User-Agent at 
the HttpClient level. Unless of course, I'm expected to keep setting it 
on each request that I build in the application? Given that typically, 
the user agent doesn't change across requests for a particular client, 
would it be feasible to introduce a way to set this up for each HttpClient?

4. The javadocs currently don't mention anything about 
connection/resource management within the HttpClient. The only mention I 
found was here[3] which states:

  "An HttpClient provides configuration information, and resource 
sharing, for all requests send through it."

I'm guessing the resource sharing implies connection reuse? How long are 
these (internal) resources maintained? Should the users of HttpClient be 
bothered about it? The reason I ask is, I don't see a "close()" kind of 
API on the HttpClient. Does it mean that the internal resource 
management isn't expected to be tied to the usage of the HttpClient 
instance?

[1] http://jdk.java.net/11/

[2] 
http://cr.openjdk.java.net/~chegar/httpclient/02/javadoc/api/java.net.http/java/net/http/package-summary.html

[3] 
http://cr.openjdk.java.net/~chegar/httpclient/02/javadoc/api/java.net.http/java/net/http/HttpClient.html

-Jaikiran



More information about the net-dev mailing list