HttpWebRequest timeout not working

Boyd Mills 0 Reputation points
2021-11-10T15:21:18.593+00:00

The following code:

                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sRequest); // Create(sQuery);
                    request.Credentials = CredentialCache.DefaultCredentials;
                    request.Timeout = 10000;
                    request.ReadWriteTimeout = 10000;


                    // Get the response.
                    Console.WriteLine("Away we go "+ sRequest );
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    Console.WriteLine("We are back");

hangs on the request.GetResponse(); When I disconnect the server. It never returns. It should return.
Running on Windows 10 using Visual Studio 2013

Please advise

Windows for business | Windows Client for IT Pros | Networking | Network connectivity and file sharing
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

Developer technologies | ASP.NET Core | Other

3 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 84,251 Reputation points
    2026-07-02T14:55:17.8566667+00:00

    HttpWebRequest is obsolete and this long standing bug probably won't be fixed. You should switch to HttpClient. the only current work around, is to use HttpWebRequest on a background thread and use a timer to kill the thread if response is not done in time.

    Was this answer helpful?

    0 comments No comments

  2. Boyd Mills 0 Reputation points
    2021-11-10T20:51:29.513+00:00

    so..... It has to be wrapped in a try - catch structure?
    Boyd

    Was this answer helpful?


  3. Jaliya Udagedara 2,856 Reputation points MVP Volunteer Moderator
    2021-11-10T20:13:59.63+00:00

    It won't return, if the resource is not returned within the time-out period, it will throw an exception of type WebException with the Status property set to WebExceptionStatus.Timeout.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.