Is there anyway to reset webclient?

假装没事ソ 提交于 2021-01-27 21:23:35

问题


Im experiencing problem with webclient to parse some very sensitive webpage. Somehow, webclient failed to do some task, and i dont even know why, no exception, no error, just simple not working (this is not the problem from the site)

I run webclient in a loop, however, only the first request was success, all the next requests are failed. When i restart the application, the same result appear, the first request always succeed, while all other request are failed.

Im sure all webclient are disposed properly, but i don't know what is the problem, is webclient saving information even after dispose?

Please guide me how to clear everything, reset everything and start brand new webclient.

for (int i = 1; i <= Count; i++)
{
    using (WebClient wc = new WebClient())
    {
        wc.Headers["Accept-Encoding"] = "gzip";
        wc.Headers["User-Agent"] = "Mozilla/4.0 (Compatible; Windows NT 5.1; MSIE 6.0) (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
        byte[] arr = wc.DownloadData(url);

        if (arr.Length > 0)
            Console.WriteLine(i.ToString() + ": SUCCESS");
        else
            Console.WriteLine(i.ToString() + ": FAILED");
    }
}

回答1:


WebClient will not share any information between instances.
You probably have a different problem.

For example, the site might ignore requests that happen too frequently.

You should run Fiddler and check exactly what is happening.

Also, try adding Thread.Sleep(5000) in the loop and see whether anything changes. (In Fiddler)



来源:https://stackoverflow.com/questions/2140176/is-there-anyway-to-reset-webclient

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!