在性能,稳定性,成熟度等方面,用于HTTP POST,GET等的最佳Java库是什么?是否有一个特定的库比其他库使用更多?
我的要求是向远程服务器提交HTTPS POST请求.我过去使用过java.net.*包以及org.apache.commons.httpclient.*包.两人都完成了工作,但我想要你的一些意见/建议.
Chr*_*ris 100
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
一些亮点功能:
Adi*_*dil 21
我会推荐Apache HttpComponents HttpClient,它是Commons HttpClient的继承者
我还建议你看看HtmlUnit.HtmlUnit是一个"用于Java程序的GUI-Less浏览器". http://htmlunit.sourceforge.net/
小智 16
我有点偏爱泽西岛.我们在所有项目中使用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客户端的HTTPClient:https://blogs.oracle.com/PavelBucek/entry/jersey_client_apache_http_client
Pab*_*jim 11
我同意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);
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请参见http://www.restlet.org/
小智 6
我可以向你推荐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));
Run Code Online (Sandbox Code Playgroud)
maven依赖
<dependency>
<groupId>net.sf.corn</groupId>
<artifactId>corn-httpclient</artifactId>
<version>1.0.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我想提到Ning Async Http客户端库。我从未使用过它,但与过去我一直使用的Apache Http Client相比,我的同事对此赞不绝口。我特别感兴趣的是,它基于高性能的异步I / O框架Netty,对此我更加熟悉并深信。
归档时间: |
|
查看次数: |
113934 次 |
最近记录: |