如何在 Commons HttpClient 3.x 中获取当前传输速率

发布于 2024-08-11 04:02:03 字数 317 浏览 9 评论 0原文

我有一些代码使用 Commons HttpClient 3.1< 执行大量 HTTP GET、POST 和 PUT /a>.我想在 GUI 中提供当前传输速度指示器,但无法找到任何方法来确定正在处理的 HttpMethod 的传输速率。

我可以轻松地计算传输时间并在完成后进行一些简单的数学计算以确定速度,但这在长时间上传或下载期间提供了糟糕的用户体验。有谁知道如何确定 Commons HttpClient 请求在处理过程中的传输速率?

I have some code that does a bunch of HTTP GETs, POSTs, and PUTs using Commons HttpClient 3.1. I'd like to provide a current transfer speed indicator in my GUI, but was unable to find any methods for determining the transfer rate of a HttpMethod being processed.

I could easily just time the transfer and do some simple math after it was complete to determine what the speed was, but this provides a bad user experience during a long upload or download. Does anyone know how to determine the transfer rate of a Commons HttpClient request while it is still being processed?

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

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

发布评论

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

评论(2

紅太極 2024-08-18 04:02:03

我没有广泛使用 HttpClient,所以可能有一个简单的钩子。但是,似乎 HttpConnection.getResponseInputStream() 返回一个简单的InputStream

要自己添加挂钩,您需要重写 HttpConnectionManager 和 HttpConnection,以返回一个跟踪读取字节数的修饰流。然后,您可以启动第二个线程来轮询该流并显示传输速率,或者(更好)每 N 个字节创建一个带有回调的流(更好,因为您不必关心并发性,并且您还可以设置 N这样回调只会针对大文件调用)。

I haven't used HttpClient extensively, so there may be a simple hook. However, it appears that HttpConnection.getResponseInputStream() returns a simple InputStream.

To add the hook yourself, you'd need to override HttpConnectionManager and HttpConnection, to return a decorated stream that keeps track of the number of bytes read. You could then spin up a second thread to poll this stream and display the transfer rate, or (better) create the stream with a callback every N bytes (better because you don't have to care about concurrency, and you can also set N such that the callback is only invoked for large files).

捎一片雪花 2024-08-18 04:02:03

更简单的钩子是扩展 HttpEntityWrapper 并覆盖 getContent() 方法:

public InputStream getContent() throws IOException {
    InputStream wrappedin = wrappedEntity.getContent();
    return new MyTransferRateInputStream(wrappedin);
}

稍后您可以将其添加为响应拦截器

httpClient.addResponseInterceptor(HttpResponseInterceptor itcp)

这样您就不需要覆盖提到的HttpConnectionManagerHttpConnection

Simpler hook would be to extend the HttpEntityWrapper and override the getContent() method:

public InputStream getContent() throws IOException {
    InputStream wrappedin = wrappedEntity.getContent();
    return new MyTransferRateInputStream(wrappedin);
}

Later you can add this as the response interceptor

httpClient.addResponseInterceptor(HttpResponseInterceptor itcp)

That way you don't need to override the mentioned HttpConnectionManager and HttpConnection

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