gpt4 book ai didi

c# - 如何将 DownloadString(url) 允许的时间限制为 500 毫秒?

转载 作者:可可西里 更新时间:2023-11-01 03:05:18 30 4
gpt4 key购买 nike

我正在编写一个程序,当 textBox1 发生变化时:

URL = "http://example.com/something/";
URL += System.Web.HttpUtility.UrlEncode(textBox1.Text);
s = new System.Net.WebClient().DownloadString(URL);

我想将 DownloadString(URL) 允许的时间限制为 500 毫秒。超过则取消。

最佳答案

没有这样的属性,但您可以轻松地扩展 WebClient:

public class TimedWebClient: WebClient
{
// Timeout in milliseconds, default = 600,000 msec
public int Timeout { get; set; }

public TimedWebClient()
{
this.Timeout = 600000;
}

protected override WebRequest GetWebRequest(Uri address)
{
var objWebRequest= base.GetWebRequest(address);
objWebRequest.Timeout = this.Timeout;
return objWebRequest;
}
}

// use
string s = new TimedWebClient {Timeout = 500}.DownloadString(URL);

关于c# - 如何将 DownloadString(url) 允许的时间限制为 500 毫秒?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12878857/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com