如何限制500毫秒允许的DownloadString(url)时间? [英] How to limit the time DownloadString(url) allowed by 500 milliseconds?

查看:130
本文介绍了如何限制500毫秒允许的DownloadString(url)时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,当textBox1更改时:

I'm writing a program that when textBox1 change:

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

我希望将DownloadString(URL)的时间限制为500毫秒.如果超过,则将其取消.

I want limit the time DownloadString(URL) allowed by 500 milliseconds. If more than, cancel it.

推荐答案

没有此类属性,但是您可以轻松扩展WebClient:

There is no such property, but you can easily extend the 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);

这篇关于如何限制500毫秒允许的DownloadString(url)时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆