Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpClient in java [closed]

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?

like image 801
computeAlot Avatar asked May 18 '11 22:05

computeAlot


People also ask

Does HttpClient need to be closed?

You do not need to explicitly close the HttpClient, however, (you may be doing this already but worth noting) you should ensure that connections are released after method execution. Edit: The ClientConnectionManager within the HttpClient is going to be responsible for maintaining the state of connections.

How do I close Java net HttpClient?

If it is just to gracefully close HttpClient at the end of the application lifecycle, System. exit(0) shall just work.

How do I close my HttpClient?

Step 2 - Start a try-finally blockStart a try-finally block, write the remaining code in the programs in the try block and close the CloseableHttpClient object in the finally block.

Is HttpClient available in Java 8?

Is there any way to use it in java 8? No, because the jdk. incubator. http module has been added since Java 9.


3 Answers

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.

like image 66
Rob Hruska Avatar answered Oct 29 '22 06:10

Rob Hruska


I highly recommend Unirest:

Unirest.post("http://httpbin.org/post")
  .queryString("name", "Mark")
  .field("last", "Polo")
  .asString()
like image 36
Paweł Krupiński Avatar answered Oct 29 '22 04:10

Paweł Krupiński


Try jcabi-http, which acts as a wrapper of JDK HttpURLConnection or Apache HttpClient:

String body = new JdkRequest("http://www.example.com")
  .uri().queryParam("id", "123").back()
  .method(Request.GET)
  .fetch()
  .body();

Check this blog post for more information: http://www.yegor256.com/2014/04/11/jcabi-http-intro.html

like image 40
yegor256 Avatar answered Oct 29 '22 06:10

yegor256