25

I want to use a simple HttpClient.

However, it appears sun.net.www.http.HttpClient is inaccessible.

Also, com.ibm.ws.http.HTTPConnection - appears to be more supporting of http server and not client. Why? Because when I create an instance of HttpConnection, it has a "getHttpResponse" to which I am supposed to write.

Anyway to use the IBM HttpConnection for HttpClient?

Or, is there any standard httpClient code that I can use?

Dimitar
  • 4,402
  • 4
  • 31
  • 47
computeAlot
  • 267
  • 1
  • 3
  • 3

5 Answers5

20

Many people use Apache's HTTPClient.

Have a look at the first few chapters of its tutorial to see if it's what you're looking for.

If you're after something simple that's already built into Java, you can look at HttpURLConnection, which you can use to build HTTP requests (example). If you need to do anything more than just simple HTTP requests, though, HTTPClient is probably the way to go.

Community
  • 1
  • 1
Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
18

I highly recommend Unirest:

Unirest.post("http://httpbin.org/post")
  .queryString("name", "Mark")
  .field("last", "Polo")
  .asString()
Paweł Krupiński
  • 1,406
  • 1
  • 14
  • 23
  • Unirest & jcabi-http (as suggested by @yegor256) seem to be the 2 most easy to use library, and the usage are pretty similar. Do you guys know any different between the two? – Hoang Tran Nov 08 '15 at 17:50
  • Those static methods are a pain when it comes to unit testing. I'm considering replacing Unirest with OkHttp in my project as I could pass in a mocked OkHttpClient object then (instead of using PowerMock as of now). Also, OkHttp comes with a MockWebServer for integration testing. – Tobias Uhmann Jan 25 '19 at 17:04
  • I think this is most easy to use client – Shuai Li May 09 '19 at 14:21
3

There is another option in using google-http-java-client.

This library provides a simple and flexible API together with a pluggable approach to use low-level HTTP libraries like java.net.HttpURLConnection or Apache HTTP Client.

Sample code for posting content to content from an InputStream to a specific URL:

HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory();
InputStream is = ...;
URL url = new URL(...);
String contentType = ...;
HttpRequest httpRequest = requestFactory.buildPostRequest(
  new GenericUrl(url), new InputStreamContent(contentType, is)
);
HttpResponse execute = httpRequest.execute();
Hachmaninow
  • 572
  • 5
  • 6
2

Try Apache's HTTPClient fluent API it's so easy in use!

// Execute a GET with timeout settings and return response content as String.
Request.Get("http://somehost/")
    .connectTimeout(1000)
    .socketTimeout(1000)
    .execute().returnContent().asString();
Nikita Koksharov
  • 10,283
  • 1
  • 62
  • 71
1

You don't need a third party class, just use java's own URL and HttpURLConnection classes.
See an example here.

S1LENT WARRIOR
  • 11,704
  • 4
  • 46
  • 60
Amit G
  • 25
  • 3