我正在编写一个程序,当 textBox1 更改时:
URL = "http://example.com/something/";
URL += System.Web.HttpUtility.UrlEncode(textBox1.Text);
s = new System.Net.WebClient().DownloadString(URL);
我想将 DownloadString(URL) 允许的时间限制为 500 毫秒。如果超过,取消它。
我正在编写一个程序,当 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);