Thread: C# and the WebClient class - application hang?

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    244

    C# and the WebClient class - application hang?

    hey guys

    i've been programming a web application using the System.Net.WebClient class.

    Code:
    static private string Download(string url)
    		{
    			WebClient web = new WebClient();
    			return web.DownloadString(url);
    		}
    after 2 or 3 downloads it happens: the WebClient stops working. after a minute of application hang i get a timeout exception. when i look at in in Wireshark, i see that the webclient isn't even sending out a request.

    i tried to replicate this problem to a 10-lines-code test-project and the problem doesn't appear after 150 times downloading google...
    since my application is lots of code splitted in 3 projects, i'm not really able to post it here.

    i also tried the async version of DownloadString. - same result...

    thanks for helping
    bye
    Last edited by Devils Child; 01-09-2009 at 05:34 AM.

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    244
    OK. (to be all clean, i start a new post)

    this is my new download function:
    Code:
    		static private string Download(string url)
    		{
    			HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    			req.Method = "GET";
    			WebResponse respon = req.GetResponse();
    			Stream res = respon.GetResponseStream();
    
    			string ret = "";
    			byte[] buffer = new byte[1024];
    			int read = 0;
    			while ((read = res.Read(buffer, 0, buffer.Length)) > 0)
    			{
    				ret += Encoding.ASCII.GetString(buffer, 0, read);
    			}
    			return ret;
    		}
    the error occurs less often, but still.
    this line:
    Code:
    WebResponse respon = req.GetResponse();
    the application hangs exactly there.

Popular pages Recent additions subscribe to a feed