WebClient不遵守超时策略

时间:2016-12-23 12:51:46

标签: c# .net haproxy

我正在使用.NET中的WebClient类发出GET请求 我创建了一个自定义webclient,它覆盖了一些属性和方法:

 public class CustomWebClient : WebClient
{
    public CookieContainer Cookies { get; private set; }
    public int Timeout { get; set; }
    public bool KeepAlive { get; set; }
    public Uri ResponseUri { get; private set; }

    public CustomWebClient()
    {
        Timeout = 30000;
        Cookies = new CookieContainer();
    }

    protected override WebRequest GetWebRequest(Uri address)
    {
            var request = base.GetWebRequest(address) as HttpWebRequest;

            if (request == null)
            {
                return base.GetWebRequest(address);
            }

            request.ContinueTimeout = 30000;
            request.KeepAlive = KeepAlive;
            request.Timeout = Timeout;
            request.CookieContainer = Cookies;
            return request;
    }

    protected override WebResponse GetWebResponse(WebRequest request)
    {
        var response = base.GetWebResponse(request);
        ResponseUri = response.ResponseUri;

        return response;
    }

    protected override WebResponse GetWebResponse(WebRequest request, IAsyncResult result)
    {
        var response = base.GetWebResponse(request, result);
        ResponseUri = response.ResponseUri;

        return response;
    }
}

然后在我的客户端类中,我发出一个简单的GET请求:

 private async Task RenewIpAddress(CustomWebClient client)
 {
        var watch = Stopwatch.StartNew();
        try
        {
            client.KeepAlive = false;
            await   client.DownloadStringTaskAsync("http://someexternalwebsite");
            client.KeepAlive = true;
        }
        catch (Exception ex) // When throws a 'The remote server returned an error: (504) Gateway Timeout.' the elapsedTime is 5 minutes
        {
            watch.Stop();

            var elapsedTime = watch.ElapsedMilliseconds;
            Debug.WriteLine("RenewIpAddress:" + TimeSpan.FromMilliseconds(elapsedTime));
            throw;
        }
    }

有时elapsedTime5 minutes。我想先杀死这个请求。它应该是TimeOut属性作业,但在某些情况下似乎不起作用。我有一个内部HAProxy作为代理来发出此请求。我可以在哪里控制这个超时?

EDIT:

抛出的删除是远程服务器返回错误:(504)网关超时。

1 个答案:

答案 0 :(得分:1)

您可以使用Task来实现。 像这样

using System;
using System.Xml;
using System.Net;
using System.Text;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            CancellationTokenSource cts = new CancellationTokenSource();
            Task task = Task.Factory.StartNew((() =>
                                   {
                                       Console.WriteLine("start");
                                       Thread.Sleep(10000); // your code
                                       Console.WriteLine("end");
                                       if (cts.IsCancellationRequested) return;
                                   }), cts.Token);
            cts.CancelAfter(1000);
            Console.WriteLine("task has been canceled");
            Console.ReadKey();
        }
    }
}