Skip to main content
 首页 » 编程设计

c#之WebClient:如何取消长时间运行的 HTTP 请求

2024年02月23日36duanxz

我正在使用 WebClient 向一个运行时间很长的 API 发出请求。但是我其实不需要知道结果,我只需要开始的过程。

这个过程是成功还是失败对我来说并不重要。所以我想在发出请求后立即断开连接。

如何断开网络客户端的连接?我不想等待 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()