Java 异步 HTTP 客户端

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1014528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:17:35  来源:igfitidea点击:

Asynchronous HTTP Client for Java

javaactionscript-3httphttpwebrequest

提问by helifreak

As a relative newbie in the Java world, I am finding many things frustratingly obtuse to accomplish that are relatively trivial in many other frameworks. A primary example is a simple solution for asynchronous http requests. Seeing as one doesn't seem to already exist, what is the best approach? Creating my own threads using a blocking type lib like httpclient or the built-in java http stuff, or should I use the newer non-blocking io java stuff - it seems particularly complex for something which should be simple.

作为 Java 世界中的一个相对新手,我发现许多在许多其他框架中相对微不足道的事情令人沮丧地难以完成。一个主要示例是异步 http 请求的简单解决方案。看到一个似乎并不存在,最好的方法是什么?使用像 httpclient 这样的阻塞类型库或内置的 java http 东西创建我自己的线程,或者我应该使用较新的非阻塞 io java 东西 - 对于应该简单的东西来说似乎特别复杂。

What I am looking for is something easy to use from a developer point of view - something similar to URLLoader in AS3 - where you simply create a URLRequest - attach a bunch of event handlers to handle the completion, errors, progress, etc, and call a method to fire it off.

我正在寻找的是从开发人员的角度来看易于使用的东西 - 类似于 AS3 中的 URLLoader - 您只需创建一个 URLRequest - 附加一堆事件处理程序来处理完成、错误、进度等,并调用一种将其关闭的方法。

If you are not familiar with URLLoader in AS3, its so super easy and looks something like this:

如果你不熟悉 AS3 中的 URLLoader,它非常简单,看起来像这样:

private void getURL(String url)
{
    URLLoader loader = new URLLoader();
    loader.addEventListener(Event.Complete, completeHandler);
    loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
    loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);

    URLRequest request = new URLRequest(url);

    // fire it off - this is asynchronous so we handle
    // completion with event handlers
    loader.load(request);
}

private void completeHandler(Event event)
{
    URLLoader loader = (URLLoader)event.target;
    Object results = loader.data;

    // process results
}

private void httpStatusHandler(Event event)
{
    // check status code
}

private void ioErrorHandler(Event event)
{
    // handle errors
}

回答by Alex Martelli

Looks like you want (a part of) NIO -- there's a good tutorial here, the asynchronous networking part starts at p. 30 and there are many useful links at the end.

像你看起来想(的一部分)NIO -有一个很好的教程在这里,在第异步联网的部分开始。30,最后有很多有用的链接。

回答by poundifdef

So, it is probably worth considering that actionscript and Java don't server the same niche. For example, Java does make some things more tedious - but usually that is to give the user more options in how, for example, an HTTP connection is executed, whereas actionscript might abstract details or possible errors away for ease of use. But, your point still stands.

因此,可能值得考虑的是 actionscript 和 Java 不服务于相同的利基市场。例如,Java 确实让一些事情变得更加乏味——但通常是在如何执行 HTTP 连接方面为用户提供更多选择,而 actionscript 可能会抽象细节或可能的错误以便于使用。但是,你的观点仍然成立。

I myself am not aware of an asynchronous HTTP client for Java. Alex Martelli's answer talks about Java's NIO, which is a good answer if you are interested in implementing the HTTP protocol in your own code. NIO will let you use sockets to connect to the web server - but then you have to manually create your own GET requests and parse the incoming HTTP headers/data.

我自己不知道 Java 的异步 HTTP 客户端。Alex Martelli 的回答谈到了 Java 的 NIO,如果您有兴趣在自己的代码中实现 HTTP 协议,这是一个很好的回答。NIO 将允许您使用套接字连接到 Web 服务器 - 但是您必须手动创建自己的 GET 请求并解析传入的 HTTP 标头/数据。

Another option is to use the java.net.URL classes - and you can find many tutorials for those online and on stackoverflow. You can wrap those in threads - so your java program has multiple threads of execution.

另一种选择是使用 java.net.URL 类 - 您可以在网上和 stackoverflow 上找到许多教程。您可以将它们包装在线程中 - 这样您的 Java 程序就有多个执行线程。

But then you run into the problem of synchronization. Which I agree, is a pain, but then it offers a more granular level of flexibility.

但是随后您遇到了同步问题。我同意,这是一种痛苦,但它提供了更细粒度的灵活性。

(I realize that this doesn't answer your question - and if anybody actually knows of a java facility to do asynchronous http requests, I'd be interested to know!)

(我意识到这并不能回答你的问题——如果有人真的知道一个 Java 工具来执行异步 http 请求,我很想知道!)

回答by cwash

AFAIK the TCPMon tool takes a similar approach to what you describe. You can take a look at the source code in their SVN browser

AFAIK TCPMon 工具采用与您描述的方法类似的方法。您可以在他们的SVN 浏览器中查看源代码

Also have a look at WGET-javafor the guts of the blocking code.

还可以查看WGET-java以了解阻塞代码的内容。

But do you have to write this in Java? There are a lot of other approaches using JRuby or Rhino to accomplish something like this easily that will run on the JVM but aren't written in Java.

但是你必须用Java写这个吗?还有很多其他方法使用 JRuby 或 Rhino 来轻松完成类似的事情,这些方法将在 JVM 上运行,但不是用 Java 编写的。

回答by Niklas

httpunitand htmlunitare 2 customizable and configurable Java http clients able to anything a browser such as simulate firefox, headless browsing, scheduled software clients and agents.

httpunithtmlunit是 2 个可定制和可配置的 Java http 客户端,能够对浏览器进行任何操作,例如模拟 Firefox、无头浏览、预定软件客户端和代理。

回答by alamar

I'd recommend firing separate threads for that.

我建议为此触发单独的线程。

回答by Jim Ferrans

If you haven't looked at it already, check out the Java 5 java.util.concurrent -- it makes multi-threaded apps much easier to develop. You can set up a ThreadPoolExecutor that manages, say, four Threads. You then feed the pool any number of tasks to complete. Each task is a Runnable. The ThreadPoolExecutor will queue up the Runnable tasks and feed them to available Threads in parallel. The Pool's afterExecute() method is called when each Runnable task completes.

如果您还没有看过它,请查看 Java 5 java.util.concurrent —— 它使多线程应用程序的开发变得更加容易。您可以设置一个 ThreadPoolExecutor 来管理四个线程。然后,您可以为池提供任意数量的任务来完成。每个任务都是一个 Runnable。ThreadPoolExecutor 会将 Runnable 任务排队并并行地将它们提供给可用的线程。当每个 Runnable 任务完成时调用 Pool 的 afterExecute() 方法。

I vividly remember writing a fetch thread pool for a web browser written in Java back in 1999, and it was a bearto get right. Last month I wrote a load tester for a web server. The tester has a ThreadPoolExecutor that has n threads, and the Runnable tasks I feed it each fetch a page using Apache HTTP Client. It took just an hour or two to get it working reasonably well. I think you'll like java.util.concurrent coupled with Apache HTTP Client, though it sounds like you'll need to do some customization for progress indication.

我清楚地记得写一个获取线程池用Java编写的早在1999年网络浏览器,这是一个熊市得到的权利。上个月我为 Web 服务器编写了一个负载测试器。测试人员有一个 ThreadPoolExecutor,它有 n 个线程,我提供给它的 Runnable 任务每个使用 Apache HTTP 客户端获取一个页面。只花了一两个小时就让它工作得相当好。我认为您会喜欢 java.util.concurrent 与 Apache HTTP 客户端的结合,尽管听起来您需要对进度指示进行一些自定义。

(Note that Apache HTTP Client does its own thread pooling, and the default configuration limits you to 20 threads max, and only two to each web server.)

(请注意,Apache HTTP 客户端执行其自己的线程池,默认配置将您限制为最多 20 个线程,每个 Web 服务器只能使用两个线程。)

Update: Here's the link to Apache HTTP Client. Be sure to read up on MultiThreadedHttpConnectionManager, it's what handles the connection pool, and it's not shown in the most basic example.

更新:这是Apache HTTP Client的链接。请务必阅读MultiThreadedHttpConnectionManager,它是处理连接池的内容,并且在最基本的示例中没有显示。

回答by Jim Ferrans

Take also a look into http://www.javaworld.com/javaworld/jw-03-2008/jw-03-asynchhttp.htmlThis article discusses async HTTP based on a HttpClient named xLightweb

另请查看http://www.javaworld.com/javaworld/jw-03-2008/jw-03-asynchhttp.html本文讨论了基于名为 xLightweb 的 HttpClient 的异步 HTTP

回答by Jim Ferrans

I just stumbled upon the asynchronous HTTP client implemented within Geronimo. You might also want to take a look it, at http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/- Caveat: the latest commit seems over a year old.

我只是偶然发现了 Geronimo 中实现的异步 HTTP 客户端。您可能还想看看它,在http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/- 警告:最新的提交似乎已经有一年多了。

Another project building an asynchronous HTTP client is xsocket: xsocket.sourceforge.net

另一个构建异步 HTTP 客户端的项目是 xsocket:xsocket.sourceforge.net

回答by Avi Flax

The Jetty HTTP clientis asynchronous.

码头HTTP客户端是异步的。

回答by Robert Campbell

Version 4.0 of Apache Commons HttpClient (now in HttpComponents/HttpCore) also support Java's NIO (non-blocking IO). I think this is your best bet.

Apache Commons HttpClient 4.0 版(现在在HttpComponents/HttpCore 中)也支持 Java 的 NIO(非阻塞 IO)。我认为这是你最好的选择。