繁体   English   中英

WebClient:如何取消长时间运行的HTTP请求?

[英]WebClient: how to cancel a long running HTTP request?

我正在使用WebClient向一个运行时间很长的API发出请求。 但我实际上并不需要知道结果,我只需要启动该过程。

如果过程成功或失败,对我来说无关紧要。 所以我想在我提出请求后立即放弃连接。

如何删除webclient的连接? 我不想等待30-60秒的回复:

var client = new WebClient();
string url = "http://example.com/SomeVeryLongRunningProcess/parameter";
client.BeginDownloadString(uri);
client.DropConnection; // how do I drop the connection before the response is received?

你可以使用: client.CancelAsync()

CancelAsync()应该可以解决问题。

我建议您使用DownloadProgressChanged事件而不是=>您将确保服务器收到您的请求并开始对其进行处理。

警告:如果不是这种情况,您还需要更新服务器代码,以便在启动过程后立即开始返回信息(HTTP标头或某些文本)。 如果没有,DownloadProgressChanged将仅在进程完成时开始接收信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM