如何使用TIdHTTP停止(取消)下载并保持连接

huangapple go评论68阅读模式
英文:

How stop (cancel) a download using TIdHTTP and keep connection

问题

以下是您要翻译的内容的中文翻译部分:

有一半的问题有一个很好的答案:https://stackoverflow.com/questions/6922787/

但是如果我使用 Abort,有时 GET 请求仍然会继续运行。

如果我使用 Disconnect(),那么下一个 GET 请求将与服务器建立新连接并花费时间。我运行 GET 请求到一个 HTTPS 服务器。HTTPS 协议大约消耗了 50% 的时间来建立连接。

我如何停止 GET 请求并保留连接以供下一个请求使用?如何清除垃圾并重置状态?

我认为我需要更改:

try
    try
        ReadBytes(LBuf, i, False);
    except
        on E: Exception do begin

            // 添加这个!
            if (E is EAbort) then
            begin
                // 设置一些标志
                // 清除缓冲区
                raise;
            end

            // RLebeau - 在 ReadBytes() 中的 ReadFromSource()
            // 可能已经用更多的字节填充了 InputBuffer,比实际请求的还多,
            // 所以这里不要提取太多字节...

            i := IndyMin(i, FInputBuffer.Size);
            FInputBuffer.ExtractToBytes(LBuf, i, False);
            if (E is EIdConnClosedGracefully) and AReadUntilDisconnect then begin
                Break;
            end else begin
                raise;
            end;
        end;
    end;

请注意,我已经将代码中的注释也进行了翻译。

英文:

There is a good answer on half of my question: https://stackoverflow.com/questions/6922787/

But if I use Abort, then sometimes the GET request will still continue to run.

If I use Disconnect() then the next GET request will establish a new connection with the server and spend time for that. I run GET requests to an HTTPS server. The HTTPS protocol consumes about 50% time just for establishing connections.

How can I stop a GET request and keep the connection for the next request? How can I sweep garbage and reset state?

I think I need to change:

 try
    try
      ReadBytes(LBuf, i, False);
    except
      on E: Exception do begin

        // ADD THIS!
        if (E is EAbort) then
        begin
          //Set some flags
          //ClearBuffers 
          raise;
        end

        // RLebeau - ReadFromSource() inside of ReadBytes()
        // could have filled the InputBuffer with more bytes
        // than actually requested, so don't extract too
        // many bytes here...

        i := IndyMin(i, FInputBuffer.Size);
        FInputBuffer.ExtractToBytes(LBuf, i, False);
        if (E is EIdConnClosedGracefully) and AReadUntilDisconnect then begin
          Break;
        end else begin                                                                                     
          raise;
        end;
      end;
    end;

答案1

得分: 0

尽管您声明,ALL引发的异常都会导致TIdHTTP停止执行当前的请求/响应循环,并将异常传播回您的代码以供处理1

1: 除了在执行远程断开时引发异常的特殊情况,远程断开预期表示响应结束。TIdHTTP会在内部处理该情况并在不重新引发异常的情况下停止读取。

我的先前评论在您的之前的问题上仍然适用:

您不能在请求进行中中止请求并期望保持连接打开,您不知道您正在离开套接字的状态或已经传输了哪些数据。没有办法清除HTTP连接并保持其开放以进行新的请求。唯一明智的做法是关闭连接。

如果您担心重新建立新的HTTPS连接太耗时,有技术可以解决这个问题,例如快速TCP打开和TLS会话重用。但Indy目前不实现这些功能。有相关的开放问题:

#213: 添加对OpenSSL会话重用的支持

#263: 添加对TCP快速打开套接字选项的支持

英文:

Despite your claim, ALL exceptions raised will make TIdHTTP stop running the current request/response cycle, and will propagate the exception back into your code for handling <sup>1</sup>.

<sup>1</sup>: except in the specific case where an exception is raised when a remote disconnect is performed, and a remote disconnect is expected to indicate the end of the response. TIdHTTP handles that condition internally and stops reading without re-raising the exception.

My earlier comment on your previous question still applies here:

> You can't abort a request midway and expect to leave the connection open, you don't know what state you are leaving the socket in or what data has already been transmitted. There is no way to clear an HTTP connection and leave it open for a new request. The only sane thing to do is close the connection.

If you are worried about re-establishing new HTTPS connections taking too long, there are technologies to address that, such as Fast TCP Open and TLS session reuse. But Indy does not implement those features at this time. There are open tickets for them:

#213: Add support for OpenSSL session reuse

#263: Add support for TCP Fast Open socket option

huangapple
  • 本文由 发表于 2023年5月24日 19:51:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76323220.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定