locked
Got error "System.Net.WebException: The operation has timed out" from time to time RRS feed

  • Question

  • User-105856378 posted

    My application use 5 threads to send HTTP requests continuously to a REST service. I keep on getting the following error from time to time

    System.Net.WebException: The operation has timed out
       at System.Net.HttpWebRequest.GetResponse()
       at LexisNexis.LTN.Analytics.Util.HttpHelper.Verb(Uri uri, Verbs verb, JObject jObject, PostParameter[] parameters, HttpStatusCode expectedHttpCode, Boolean readResponseBody) in c:\NingjunWS\Proof of Concept\CaatLib\LexisNexis.LTN.Analytics\Util\HttpHelper.cs:line 104

    I Google around and see many people talk about that but nothing work for me

    Here is my code snippet

                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);

               httpWebRequest.Method = verb.ToString();

               httpWebRequest.Accept = "application/json"; //accept json

              

               using (Stream parametersStream = httpWebRequest.GetRequestStream())

                       parametersStream.Write(parameterData, 0, parameterData.Length);

               

       try

               {

                   using (HttpWebResponse resp = (HttpWebResponse) httpWebRequest.GetResponse())

                   { // The above line throw the exception System.Net.WebException: The operation has timed out sporadically

                        

                       // read response body

                       using (Stream dataStream = resp.GetResponseStream())

                       using (StreamReader streamReader = new StreamReader(dataStream, Encoding.UTF8))

                       {

                           string ret = streamReader.ReadToEnd();

                           return ret;

                       }

                   }

               }

               catch (WebException ex)

               {

                   using (HttpWebResponse response = (HttpWebResponse) ex.Response)

                   {

                       if (response == null) throw;

                       string errorMsg = GetErrorMessage(response);

                       throw new RestException(response.StatusCode, errorMsg, ex);

                   }

               }

           }

    Friday, May 16, 2014 12:04 PM

Answers

  • User-417640953 posted

    Hi ningjunwang2,

    Thank you post the issue to asp.net forum.

    For your issue, I suggest you note below points.

    # Increase the timeout property of HttpWebRequest like below.

     var request = (HttpWebRequest)WebRequest.Create(Url);
     request.Timeout = 1000000;

    # Try to increate the wcf service receiveTimeout and sendTimeout like below.

     <binding name="longTimeoutBinding" receiveTimeout="00:10:00" sendTimeout="00:10:00">
            <security mode="None"/>
          </binding>
    

    Hope that helps, thanks.

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 21, 2014 3:36 AM

All replies

  • User364480375 posted

    First make sure you close your HTTP connections.

    you can try increasing maxRequestLength int the Httpruntime section in web.config.

    Monday, May 19, 2014 3:39 AM
  • User-105856378 posted

    Form the code I sent you can see that I do close all connections with the "using" statement.

    Do you suggest to increase maxRequestLength in the REST service side or client side? I do not have any control on the service side because it is a third party REST service written in JAVA.

    Monday, May 19, 2014 6:12 PM
  • User-105856378 posted

    I set the following on the client side and still got time out exception

     

      <system.web>
        <httpRuntime maxRequestLength="4096000" />
      </system.web>

     

     

    Monday, May 19, 2014 6:40 PM
  • User-105856378 posted

     <system.web>
        <httpRuntime maxRequestLength="4096000" />
      </system.web>

    Monday, May 19, 2014 6:40 PM
  • User-417640953 posted

    Hi ningjunwang2,

    Thank you post the issue to asp.net forum.

    For your issue, I suggest you note below points.

    # Increase the timeout property of HttpWebRequest like below.

     var request = (HttpWebRequest)WebRequest.Create(Url);
     request.Timeout = 1000000;

    # Try to increate the wcf service receiveTimeout and sendTimeout like below.

     <binding name="longTimeoutBinding" receiveTimeout="00:10:00" sendTimeout="00:10:00">
            <security mode="None"/>
          </binding>
    

    Hope that helps, thanks.

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 21, 2014 3:36 AM
  • User-105856378 posted

    Your solution work

     var request = (HttpWebRequest)WebRequest.Create(Url);
    request.Timeout = 1000000;

     

    Thanks a lot.

    Thursday, May 22, 2014 9:57 AM