如何中止HttpWebRequest(来自另一个线程)?

时间:2015-06-26 08:56:17

标签: c# httpwebrequest

我喜欢在必要时从另一个线程中止HttpWebRequest。这是正确的方法吗?

    public void Abort()
    {
        if (request != null)
        {
            try { request.ReadWriteTimeout = 0; } catch { }
            try { request.Timeout = 0; } catch { }
            try { request.Abort(); } catch { }
        }
    }

1 个答案:

答案 0 :(得分:0)

我相信它会为你节省一些努力将一切都放在一个try / catch中。另请看这个:Is it possible to abort a Task like aborting a Thread (Thread.Abort method)?

public void Abort()
{
    if (request != null)
    {
        try 
        { 
            request.ReadWriteTimeout = 0; } catch { }
            request.Timeout = 0;
            request.Abort(); 
        } catch {//Some error}
    }
}