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.
This library provides a simple and flexible API together with a pluggable approach to use low-level HTTP libraries like java.net.HttpURLConnection or Apache HTTP Client.
Sample code for posting content to content from an InputStream to a specific URL:
HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory();
InputStream is = ...;
URL url = new URL(...);
String contentType = ...;
HttpRequest httpRequest = requestFactory.buildPostRequest(
new GenericUrl(url), new InputStreamContent(contentType, is)
);
HttpResponse execute = httpRequest.execute();
// Execute a GET with timeout settings and return response content as String.
Request.Get("http://somehost/")
.connectTimeout(1000)
.socketTimeout(1000)
.execute().returnContent().asString();
// Execute a GET with timeout settings and return response content as String.
Request.Get("http://somehost/")
.connectTimeout(1000)
.socketTimeout(1000)
.execute().returnContent().asString();
发布评论
评论(5)
许多人使用 Apache 的 HTTPClient。
查看其教程的前几章 看看这是否是您要找的。
如果您正在寻找 Java 中已内置的简单内容,您可以查看 HttpURLConnection,您可以使用它来构建 HTTP 请求 (示例)。不过,如果您需要执行的不仅仅是简单的 HTTP 请求,那么 HTTPClient 可能是最佳选择。
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.
我强烈推荐 Unirest:
I highly recommend Unirest:
还有另一种选择是使用 google-http-java-client。
该库提供了简单而灵活的 API 以及可插入方法来使用低级 HTTP 库(如 java.net.HttpURLConnection 或 Apache HTTP Client)。
将内容从 InputStream 发布到特定 URL 的示例代码:
There is another option in using google-http-java-client.
This library provides a simple and flexible API together with a pluggable approach to use low-level HTTP libraries like java.net.HttpURLConnection or Apache HTTP Client.
Sample code for posting content to content from an InputStream to a specific URL:
尝试 Apache 的 HTTPClient Fluent API 使用起来非常简单!
Try Apache's HTTPClient fluent API it's so easy in use!
不需要第三方类,只需使用java自己的
URL
和HttpURLConnection
类即可。请参阅此处的示例。
You don't need a third party class, just use java's own
URL
andHttpURLConnection
classes.See an example here.