Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebClient is very slow

I have problem with Webclient.

It is very slow. It takes about 3-5 seconds to downloadString from one website. I don't have any network problems.

This is my Modifed WebClient.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace StatusChecker
{
    class WebClientEx: WebClient
    {
        public CookieContainer CookieContainer { get; private set; }

        public WebClientEx()
        {
            CookieContainer = new CookieContainer();

            ServicePointManager.Expect100Continue = false;
            Encoding = System.Text.Encoding.UTF8;

            WebRequest.DefaultWebProxy = null;
            Proxy = null;
        }

        public void ClearCookies()
        {
            CookieContainer = new CookieContainer();
        }

        protected override WebRequest GetWebRequest(Uri address)
        {

            var request = base.GetWebRequest(address);
            if (request is HttpWebRequest)
            {
                (request as HttpWebRequest).CookieContainer = CookieContainer;
            }
            return request;
        }
    }
}

UPDATE: In wireshark I saw that single DownladString is sending and receiving few thousands packets.

like image 293
Hooch Avatar asked Aug 08 '11 21:08

Hooch


People also ask

Is there an alternative to WebClient?

NET 4.5 platform the community developed an alternative. Today, RestSharp is one of the only options for a portable, multi-platform, unencumbered, fully open-source HTTP client that you can use in all of your applications. It combines the control of HttpWebRequest with the simplicity of WebClient .

What is the use of WebClient?

The WebClient class provides common methods for sending data to or receiving data from any local, intranet, or Internet resource identified by a URI. The WebClient class uses the WebRequest class to provide access to resources.

What does WebClient Downloadstring do?

This method retrieves the specified resource. After it downloads the resource, the method uses the encoding specified in the Encoding property to convert the resource to a String. This method blocks while downloading the resource.

How do you post data on WebClient?

The WebClient class uses the WebRequest class to provide access to resources. WebClient instances can access data with any WebRequest descendant registered with the WebRequest. RegisterPrefix method. UploadString Sends a String to the resource and returns a String containing any response.


2 Answers

There are a few options if it is related to the initial proxy settings being checked:

  1. Disable the automatic proxy detection settings in Internet Explorer
  2. Set the proxy to null:

    WebClient.Proxy = null

  3. On application startup set the default webproxy to null:

    WebRequest.DefaultWebProxy = null;

In older .NET code instead of setting to null, you used to write this (but null is now preferred):

webclient.Proxy = GlobalProxySelection.GetEmptyWebProxy();
like image 92
juFo Avatar answered Oct 19 '22 13:10

juFo


There may be two issues at hand here (that I've also noticed in my own programs previously):

  • The first request takes an abnormally long time: This occurs because WebRequest by default detects and loads proxy settings the first time it starts, which can take quite a while. To stop this, simply set the proxy property (WebRequest.Proxy) to null and it'll bypass the check (provided you can directly access the internet)
  • You can't download more than 2 items at once: By default, you can only have 2 simultaneous HTTP connections open. To change this, set ServicePointManager.DefaultConnectionLimit to something larger. I usually set this to int.MaxValue (just make sure you don't spam the host with 1,000,000 connections).
like image 32
foxy Avatar answered Oct 19 '22 12:10

foxy