5

The below piece of code is giving Error Message : "The operation has timed out" Error Sourse : at System.Net.httpWebRequest.GetResponse()

This method is calling a URL and fetching the response object.

NOTE : This is all working fine in my end..but when i send the same code to production..it shows time oout errors

public GetUpdatedInventoryUnitValues(Vehicle aeVehicle)
{
            WebRequest oWebRequest = null;
            StringBuilder oStringBuilder = null;
            StreamReader oStreamReader = null;
            dcDealerDetails = new Dictionary<string, string>();

            MSRP = string.Empty;
            NetPrice = string.Empty;
            string strLine = string.Empty;
            string strURL = GetUpdatedInventoryUnitValues.GetFormattedURL(aeVehicle);

            try
            {
                /* Open the requested URL */
                oWebRequest = WebRequest.Create(strURL);
                oWebRequest.Method = "GET";
                oWebRequest.ContentType = "application/xml";
                /* Get the stream from the returned web response */
                oStreamReader = new StreamReader(oWebRequest.GetResponse().GetResponseStream());
                /* Get the stream from the returned web response */
                oStringBuilder = new StringBuilder();
                /* Read the stream a line at a time and place each one into the stringbuilder  */
                while ((strLine = oStreamReader.ReadLine()) != null)
                {
                    /* Ignore blank lines */
                    if (strLine.Length > 0)
                        oStringBuilder.Append(strLine);
                }

                string[] tempArray = null;
                string[] tempNextArray = null;
                //Split string by semicolon as a separater
                tempArray = Data.SplitString(oStringBuilder.ToString(), new char[] { ';' });

                if (tempArray != null)
                {
                    foreach (string invUnits in tempArray)
                    {
                        //Split string by '=' as a separater
                        tempNextArray = Data.SplitString(invUnits, new char[] { '=' });

                        if (tempNextArray != null && tempNextArray.Length == 2)
                        {
                            switch (tempNextArray[0].ToLower())
                            {
                                //case "msrp":
                                //    MSRP = Data.RemoveDoubleCode(tempNextArray[1]);
                                //    break;
                                case "netprice":
                                    NetPrice = Data.RemoveDoubleCode(tempNextArray[1]);
                                    break;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorLog.ErrorMessage = ErrorLog.Separator;
                ErrorLog.ErrorMessage = "Exception during posting data to another application .";
                ErrorLog.ErrorMessage = "ERROR MESSAGE : " + ex.Message;
                ErrorLog.ErrorMessage = "ERROR SOURCE: " + ex.StackTrace.ToString();

            }
            finally
            {
                if (oStreamReader != null)
                {
                    oStreamReader.Close();
                }
                if (oWebRequest != null)
                {
                    oWebRequest = null;
                }
            }
        }

Please suggest what am i doing wrong or missing?

4
  • Are you sure your request is good and it doesn't timeout out of the code? I mean have you tried to launch it directly in your browser? Commented Sep 20, 2012 at 12:57
  • It is all working fine in my end..but when i send the same code to production..it shows time oout errors Commented Sep 20, 2012 at 12:58
  • What are the differences between your developpement platform and your production platform? Is there any firewall or something like that which could stop your request? Commented Sep 20, 2012 at 13:01
  • Not sure if firewall is stopping production site..seems to be a potential cause in that case.. Commented Sep 20, 2012 at 13:05

3 Answers 3

17

Are you perhaps finding that the first couple of requests are okay, and then they start timing out? If so, I suspect this is the problem:

oStreamReader = new StreamReader(oWebRequest.GetResponse().GetResponseStream());

You're fetching the response, but never disposing of it. You should use:

using (var response = oWebRequest.GetResponse())
{
    ...
}

In fact, you can get rid of your finally block entirely if you use using statements throughout.

As an aside, this is a pretty long method - 77 lines! - and worse, it looks like it's actually a constructor:

  • Try to split it out into smaller, more easily understood, more easily testable chunks
  • Try to avoid doing a lot of work in constructors
Sign up to request clarification or add additional context in comments.

8 Comments

..This is working fine with my 250 calls in loop..but in production its almost all are timeouts..On code part..yes i should be doing it..
@RatanSharma: What URL are you fetching from production? Is it possible that it really is just timing out?
The above function is from the tool..which is running in production site...the function is calling a URL which is giving some response..This is working fine when i run the tool in my system , but in production server when i am running the same it is timming out..
About the URL where i am posting and getting the response also working without any issues.
@RatanSharma: You're getting the response from your system - but if you fetch it from your production system (e.g. on a browser), what happens?
|
2

Just sharing experience.

I was getting the same error "Operation Timed Out".

I have tried using both WebClient And WebRequest (set Timeout also) but was still getting error.

Reason was that I was not disposing the response.

So I used as mentioned above :

using (var response = oWebRequest.GetResponse())

{
    ...
}

And it solved the problem...

Comments

1

I personnally use this code for one of my program and it works perfect:

    WebRequest webRequest = WebRequest.Create(requestUri);
    webRequest.Credentials = new NetworkCredential(login, password);
    WebResponse webResponse = webRequest.GetResponse();
    Stream response = webResponse.GetResponseStream();
    StreamReader reader = new StreamReader(response);

So i think it doesn't come from your code but from your production platform.

3 Comments

If you're not disposing of responses then it's not working perfectly - you're just getting lucky.
By using your posted code after the end of the using it disposes of the response?
Yes. You should use using statements for pretty much anything which implements IDisposable, to ensure clean-up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.