java中的HttpClient

发布于 2024-11-08 18:58:39 字数 1539 浏览 0 评论 0 原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

贩梦商人 2024-11-15 18:58:39

许多人使用 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.

攒一口袋星星 2024-11-15 18:58:39

我强烈推荐 Unirest

Unirest.post("http://httpbin.org/post")
  .queryString("name", "Mark")
  .field("last", "Polo")
  .asString()

I highly recommend Unirest:

Unirest.post("http://httpbin.org/post")
  .queryString("name", "Mark")
  .field("last", "Polo")
  .asString()
迷途知返 2024-11-15 18:58:39

还有另一种选择是使用 google-http-java-client

该库提供了简单而灵活的 API 以及可插入方法来使用低级 HTTP 库(如 java.net.HttpURLConnection 或 Apache HTTP Client)。

将内容从 InputStream 发布到特定 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();

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:

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();
半寸时光 2024-11-15 18:58:39

尝试 Apache 的 HTTPClient Fluent API 使用起来非常简单!

// Execute a GET with timeout settings and return response content as String.
Request.Get("http://somehost/")
    .connectTimeout(1000)
    .socketTimeout(1000)
    .execute().returnContent().asString();

Try Apache's HTTPClient fluent API it's so easy in use!

// Execute a GET with timeout settings and return response content as String.
Request.Get("http://somehost/")
    .connectTimeout(1000)
    .socketTimeout(1000)
    .execute().returnContent().asString();
眼泪都笑了 2024-11-15 18:58:39

不需要第三方类,只需使用java自己的URLHttpURLConnection类即可。
请参阅此处的示例。

You don't need a third party class, just use java's own URL and HttpURLConnection classes.
See an example here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文