The underlying connection was closed: An unexpected error occurred on a receive

Posted by Dave on Stack Overflow See other posts from Stack Overflow or by Dave
Published on 2011-06-21T03:09:06Z Indexed on 2011/06/21 8:22 UTC
Read the original article Hit count: 307

Filed under:
|
|
|

Using a console app which runs as a scheduled task on Azure, to all a long(ish) running webpage on Azure.

Problem after a few minutes: The underlying connection was closed: An unexpected error occurred on a receive

 //// used as WebClient doesn't support timeout
public class ExtendedWebClient : WebClient
{
    public int Timeout { get; set; }

    protected override WebRequest GetWebRequest(Uri address)
    {
        HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);
        if (request != null)
            request.Timeout = Timeout;
        request.KeepAlive = false;
        request.ProtocolVersion = HttpVersion.Version10;
        return request;
    }

    public ExtendedWebClient()
    {
        Timeout = 1000000; // in ms.. the standard is 100,000
    }
}

class Program
{
    static void Main(string[] args)
    {
        var taskUrl = "http://www.secret.com/secret.aspx";
        // create a webclient and issue an HTTP get to our url
        using (ExtendedWebClient httpRequest = new ExtendedWebClient())
        {
            var output = httpRequest.DownloadString(taskUrl);
        }
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET