HttpWebRequest.GetResponse() - Timeout exception

This is a discussion on HttpWebRequest.GetResponse() - Timeout exception within the C# Programming forums, part of the General Programming Boards category; hi! i'm programming an application, which relies on downloading stuff from the i-net. either Code: new WebClient().DownloadString("http://.....") or this version ...

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

    Thumbs down HttpWebRequest.GetResponse() - Timeout exception

    hi!
    i'm programming an application, which relies on downloading stuff from the i-net.

    either
    Code:
    new WebClient().DownloadString("http://.....")
    or this version of it
    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;
    		}
    are causing a timeout exception after a minute of application hang. looking at it in wireshark, you see that no request has been submitted. the trick is: this bug appears sporadically every 3-4 times downloading something.

    plz help me

  2. #2
    Registered User
    Join Date
    Apr 2009
    Posts
    1

    Cool

    Hi There,

    Try closing the response everytime.

    Code:
    WebResponse respon = req.GetResponse();
    try 
    { 
      // do stuff 
    }
    finally
    {
      respon.Close();
    }
    or better:

    Code:
    using (WebResponse respon = req.GetResponse())
    {
      // do stuff
    }
    Dispose() and Close() are called automatically

    Cheers,

    Melle
    Last edited by melle; 04-08-2009 at 09:03 AM. Reason: added example code

  3. #3
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Just a small point, rather than manually having to loop through the received bytes, you could simply use a StreamReader instead. Example:

    Code:
            private static String Download(String url)
            {
                String str = String.Empty;
    
                try
                {
                    WebRequest request = WebRequest.Create(url);
                    WebResponse response = request.GetResponse();
                    Stream stream = response.GetResponseStream();
                    StreamReader reader = new StreamReader(stream, Encoding.Default);
                    str = reader.ReadToEnd();
                    reader.Close();
                    stream.Flush();
                    stream.Close();
                    response.Close();
                }
                catch { }
    
                return str;
            }

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    1
    I had the exact same problem, and I fixed it by placing the WebResponse object in a using statement. I was not disposing of it. Just wanted to verify the fix by melle.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exception handling in a large project
    By EVOEx in forum C++ Programming
    Replies: 7
    Last Post: 01-25-2009, 07:33 AM
  2. exception handling
    By coletek in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2009, 05:28 PM
  3. Replies: 8
    Last Post: 03-10-2008, 12:57 PM
  4. Handle C++ exception and structured exception together
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 01-24-2008, 09:21 PM
  5. Signal and exception handling
    By nts in forum C++ Programming
    Replies: 23
    Last Post: 11-15-2007, 02:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21