Java uses the HttpClient
class and contained methods for web requests and responses (formerly used HttpUrlConnection
class)
HttpUrlConnection
Pros: No library imports required;
Cons: Limited functionality compared to other libraries; Many deprecated features;
Common request-related methods: openConnection()
to create instance (of object, not connection itself); .setRequestMethod()
with the argument being the desired request (GET
, DELETE
, TRACE
, etc); getHeaderField()
for reading headers from the calling connection; setConnectTimeout()
(in milliseconds); HttpCookie.parse()
HttpClient
Pros: multiple threads in req/res; easier to maintain; optional asynchronicity;
Common request-related methods: .header()
/.headers()
(depending on if using one pair per or multiple pairs of key-values); timeout()
(with #, then units as args); .newBuilder()
to create a new builder instance which has many related methods;