在性能、稳定性和成熟度等方面,使用哪个Java库进行HTTP POST、GET等操作最佳?是否有一种特定的库比其他库更常用?
我的要求是向远程服务器提交HTTPS POST请求。我过去曾使用java.net.*包和org.apache.commons.httpclient.*包。这两种库都能完成工作,但我想听听您的意见/建议。
我个人认为:Apache HTTP Client
使用示例:
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();
}
}
}
一些值得注意的特点:
我建议使用Apache HttpComponents HttpClient,它是Commons HttpClient的后继者。
我还推荐看一下HtmlUnit。HtmlUnit是Java程序的“无界面浏览器”。 http://htmlunit.sourceforge.net/
我比较偏爱Jersey,我们在所有项目中都使用1.10版本,并且没有遇到过无法解决的问题。
以下是我喜欢它的一些原因:
事实上,HTTPClient和Jersey在实现和API上非常相似。 Jersey还有扩展程序,可以支持HTTPClient。
一些Jersey 1.x的代码示例: https://blogs.oracle.com/enterprisetechtips/entry/consuming_restful_web_services_with
http://www.mkyong.com/webservices/jax-rs/restful-java-client-with-jersey-client/
使用Jersey Client的HTTPClient: https://blogs.oracle.com/PavelBucek/entry/jersey_client_apache_http_client
我同意httpclient是一种标准——但我想你正在寻找选项,因此...
Restlet提供了一个专门为与Restful Web服务交互而设计的http客户端。
示例代码:
Client client = new Client(Protocol.HTTP);
Request r = new Request();
r.setResourceRef("http://127.0.0.1:8182/sample");
r.setMethod(Method.GET);
r.getClientInfo().getAcceptedMediaTypes().add(new Preference<MediaType>(MediaType.TEXT_XML));
client.handle(r).getEntity().write(System.out);
详细信息请参见https://restlet.talend.com/
我想提到Ning Async Http Client Library。虽然我从来没有使用过它,但是我的同事对它比起我一直使用的Apache Http Client大加赞赏。尤其让我感兴趣的是它基于Netty,这是一个高性能的异步I/O框架。我更加熟悉Netty并且对其非常看重。
我可以向你推荐corn-httpclient。它简单易用,速度快,并且对大多数情况都足够。
HttpForm form = new HttpForm(new URI("http://localhost:8080/test/formtest.jsp"));
//Authentication form.setCredentials("user1", "password");
form.putFieldValue("input1", "your value");
HttpResponse response = form.doPost();
assertFalse(response.hasError());
assertNotNull(response.getData());
assertTrue(response.getData().contains("received " + val));
Maven 依赖
<dependency>
<groupId>net.sf.corn</groupId>
<artifactId>corn-httpclient</artifactId>
<version>1.0.0</version>
</dependency>