Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best Java library to use for HTTP POST, GET etc.? [closed]

Tags:

java

http

What is the best Java library to use for HTTP POST, GET etc. in terms of performance, stability, maturity etc.? Is there one particular library that is used more than others?

My requirements are submitting HTTPS POST requests to a remote server. I have used the java.net.* package in the past as well as org.apache.commons.httpclient.* package. Both have got the job done, but I would like some of your opinions/recommendations.

like image 388
rmcc Avatar asked Aug 24 '09 13:08

rmcc


People also ask

Which HTTP client is best in Java?

If we do not want to add any external libraries, Java's native HTTPClient is the first choice for Java 11+ applications. Spring WebClient is the preferred choice for Spring Boot applications more importantly if we are using reactive APIs.

What is HTTP POST Java?

The HTTP POST method is defined in section 9.5 of RFC2616: The POST method is used to request that the origin server accept the entity enclosed in the request as a new subordinate of the resource identified by the Request-URI in the Request-Line.

What are HTTP libraries?

This library provides methods to handle typical apps web requests, such as calls to RESTful APIs and image downloads. It also simplifies the management of the HTTP lifecycle by providing asynchronous calls, transparent HTTP caching, automatic scheduling of network requests and request prioritization.


1 Answers

imho: Apache HTTP Client

usage example:

import org.apache.commons.httpclient.*; import org.apache.commons.httpclient.methods.*; import org.apache.commons.httpclient.params.HttpMethodParams;  import java.io.*;  public class HttpClientTutorial {    private static String url = "http://www.apache.org/";    public static void main(String[] args) {     // Create an instance of HttpClient.     HttpClient client = new HttpClient();      // Create a method instance.     GetMethod method = new GetMethod(url);      // Provide custom retry handler is necessary     method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,              new DefaultHttpMethodRetryHandler(3, false));      try {       // Execute the method.       int statusCode = client.executeMethod(method);        if (statusCode != HttpStatus.SC_OK) {         System.err.println("Method failed: " + method.getStatusLine());       }        // Read the response body.       byte[] responseBody = method.getResponseBody();        // Deal with the response.       // Use caution: ensure correct character encoding and is not binary data       System.out.println(new String(responseBody));      } catch (HttpException e) {       System.err.println("Fatal protocol violation: " + e.getMessage());       e.printStackTrace();     } catch (IOException e) {       System.err.println("Fatal transport error: " + e.getMessage());       e.printStackTrace();     } finally {       // Release the connection.       method.releaseConnection();     }     } } 

some highlight features:

  • Standards based, pure Java, implementation of HTTP versions 1.0 and 1.1
    • Full implementation of all HTTP methods (GET, POST, PUT, DELETE, HEAD, OPTIONS, and TRACE) in an extensible OO framework.
    • Supports encryption with HTTPS (HTTP over SSL) protocol.
    • Granular non-standards configuration and tracking.
    • Transparent connections through HTTP proxies.
    • Tunneled HTTPS connections through HTTP proxies, via the CONNECT method.
    • Transparent connections through SOCKS proxies (version 4 & 5) using native Java socket support.
    • Authentication using Basic, Digest and the encrypting NTLM (NT Lan Manager) methods.
    • Plug-in mechanism for custom authentication methods.
    • Multi-Part form POST for uploading large files.
    • Pluggable secure sockets implementations, making it easier to use third party solutions
    • Connection management support for use in multi-threaded applications. Supports setting the maximum total connections as well as the maximum connections per host. Detects and closes stale connections.
    • Automatic Cookie handling for reading Set-Cookie: headers from the server and sending them back out in a Cookie: header when appropriate.
    • Plug-in mechanism for custom cookie policies.
    • Request output streams to avoid buffering any content body by streaming directly to the socket to the server.
    • Response input streams to efficiently read the response body by streaming directly from the socket to the server.
    • Persistent connections using KeepAlive in HTTP/1.0 and persistance in HTTP/1.1
    • Direct access to the response code and headers sent by the server.
    • The ability to set connection timeouts.
    • HttpMethods implement the Command Pattern to allow for parallel requests and efficient re-use of connections.
    • Source code is freely available under the Apache Software License.
like image 193
Chris Avatar answered Oct 02 '22 18:10

Chris