The connection was unexpectedly closed by C # after a long time

Hi, I created a scanner for the site. After about 3 hours of scanning, my application stopped at WebException. below is my code in C #. The client has a predefined WebClientobject that will be deleted each time the gameDoc is already processed. gameDoc is an object HtmlDocument(from HtmlAgilityPack)

while (retrygamedoc)
{
    try
    {
        gameDoc.LoadHtml(client.DownloadString(url)); // this line caused the exception
        retrygamedoc = false;
    }
    catch
    {
        client.Dispose();
        client = new WebClient();

        retrygamedoc = true;
        Thread.Sleep(500);
    }
}

I tried using the code below (to keep a fresh web client) from this answer

while (retrygamedoc)
{
    try
    {
        using (WebClient client2 = new WebClient())
        {
            gameDoc.LoadHtml(client2.DownloadString(url)); // this line cause the exception
            retrygamedoc = false;
        }
    }
    catch
    {
        retrygamedoc = true;
        Thread.Sleep(500);
    }
}

but the result is still the same. Then I use StreamReader and the result remains unchanged! below is my code using StreamReader.

while (retrygamedoc)
{
    try
    {
        // using native to check the result
        HttpWebRequest webreq = (HttpWebRequest)WebRequest.Create(url);
        string responsestring = string.Empty;
        HttpWebResponse response = (HttpWebResponse)webreq.GetResponse(); // this cause the exception
        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
        {
            responsestring = reader.ReadToEnd();
        }
        gameDoc.LoadHtml(client.DownloadString(url));

        retrygamedoc = false;
    }
    catch
    {
        retrygamedoc = true;
        Thread.Sleep(500);
    }
}

? , , , 1000 . The request was aborted: The connection was closed unexpectedly., ConnectionClosed

PS. .

:

, . , , , . , .

:

, 1300 -, The request was aborted: The connection was closed unexpectedly., - .

+4
1

ConnectionClosed (, ,), , , . , .

shenanigans , , .

+4

Source: https://habr.com/ru/post/1525022/


All Articles