什么是用于HTTP POST,GET等的最佳Java库? [英] What is the best Java library to use for HTTP POST, GET etc.?

查看:158
本文介绍了什么是用于HTTP POST,GET等的最佳Java库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在性能,稳定性,成熟度等方面,用于HTTP POST,GET等的最佳Java库是什么?是否有一个特定的库比其他库使用更多?

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?

我的要求是向远程服务器提交HTTPS POST请求。我过去使用过java.net。*包以及org.apache.commons.httpclient。*包。两个人都完成了工作,但我想要你的一些意见/建议。

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.

推荐答案

imho: Apache HTTP客户端

用法示例:

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();
    }  
  }
}

一些亮点功能:


  • 基于标准的纯Java,HTTP版本的实现1.0
    和1.1



    • 可扩展OO框架中完全实现所有HTTP方法(GET,POST,PUT,DELETE,
      HEAD,OPTIONS和TRACE)。

    • 支持使用HTTPS(HTTP over SSL)协议进行加密。

    • 精细的非标准配置和跟踪。

    • 通过HTTP代理进行透明连接。

    • 通过HTTP代理通过CONNECT
      方法进行隧道连接。

    • 通过SOCKS代理(版本4和5)使用透明连接
      本机Java套接字支持。

    • 使用Basic,Digest和加密NTLM(NT
      Lan Manager)方法进行身份验证。

    • 用于自定义身份验证方法的插件机制。

    • 用于上传大文件的多部分表单POST。

    • 可插入的安全套接字实现,使
      更容易使用第三方解决方案

    • 连接管理支持在多线程
      应用程序中使用。支持设置
      最大总连接数以及
      每个主机的最大连接数。
      检测并关闭失效连接。

    • 自动Cookie处理,用于读取
      服务器中的Set-Cookie:标头并将其发送回
      Cookie :适当的标头。

    • 自定义cookie策略的插件机制。

    • 请求输出流以避免缓冲任何内容正文
      直接流到套接字到
      服务器。

    • 响应输入流有效地读取响应体
      直接从套接字流到服务器

    • 在HTTP / 1.0中使用KeepAlive的持久连接和HTTP / 1.1中的持久性

    • 直接访问发送的响应代码和标头服务器。

    • 设置连接超时的能力。

    • HttpMethods实现命令模式以允许并行的
      请求和有效的re - 使用
      连接。

    • 源代码可在Apache软件许可下免费获得。

    • 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.

      这篇关于什么是用于HTTP POST,GET等的最佳Java库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆