Operation Timeout Error: HttpWebRequest and HttpWebResponse objects

I took over the build of an application.  Part of the code base needs to ensure that it has an internet connection out to a few url's.  the person before me is using HTTPWebRequest and HTTPWebResponse objects and I'm not certain but I don't think his code ever worked.  He has it in a try/catch block and he is doing a bunch of other stuff that is very similar if it DOESN'T have a connection that unless he specifically looked in his log file I don't think he would have realized it.  also, it doesn't show up in the event viewer (again, because in a try/catch block)

Anyway, I've confirmed via pasting the URL into IE that both the URL's are up and can be connected to.  

So why do I keep "Operation Timed Out" errors?  Is there a better way to do this?

I've attached the function below.
public static void SaveWebToCache(string url, int durationSeconds, bool cacheStale)
    {
        // If we are using an overriding proxy send the request to that instead of
        // sending it to the direct source

        // If we are offline do not process this request
        System.Diagnostics.Debug.WriteLine(HttpContext.Current.Application["OfflineCount"]);
        System.Diagnostics.Debug.WriteLine(HttpContext.Current.Application["OfflineErrorMax"]);
        if (Convert.ToInt32(HttpContext.Current.Application["OfflineCount"]) >= Convert.ToInt32(HttpContext.Current.Application["OfflineErrorMax"]))
        {
            if (Convert.ToDateTime(HttpContext.Current.Application["OfflineDate"]) > DateTime.Now)
            {
                HttpContext.Current.Application["OfflineCount"] = 0;    // Attempt to connect again
                return;
            }
        }


        string proxyUrl = url;
        if (WebConfigurationManager.AppSettings["Proxy2URL"].ToString() != "")
        {
            proxyUrl = WebConfigurationManager.AppSettings["Proxy2URL"].ToString();
            proxyUrl += PROXY_ASPX;
            proxyUrl += "?url=" + Uri.EscapeDataString(url);
            proxyUrl += "&dur=" + durationSeconds.ToString();
        }

        // Query the web page. If the page is offline then return what is a
        bool success = false;
        Stream streamHttp = null;
        try
        {
            HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(proxyUrl);
            webReq.Timeout = Convert.ToInt32(System.Web.Configuration.WebConfigurationManager.AppSettings["TimeOut"]);
            HttpWebResponse webResp = (HttpWebResponse)webReq.GetResponse();
            streamHttp = webResp.GetResponseStream();
            success = true;
        }
        catch (Exception exError)
        {
            success = false;
            cimLog.WriteLog(exError, cimLog.LogType.Warning);
            ProcessOffline();
        }

        // If we fail and we are using the Proxy, try again with the actual URL
        if (!success)
        {
            if (WebConfigurationManager.AppSettings["Proxy2URL"].ToString() != "")
            {
                try
                {
                    HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(url);
                    webReq.Timeout = Convert.ToInt32(System.Web.Configuration.WebConfigurationManager.AppSettings["TimeOut"]);
                    HttpWebResponse webResp = (HttpWebResponse)webReq.GetResponse();
                    streamHttp = webResp.GetResponseStream();
                    success = true;
                }
                catch (Exception exError)
                {
                    success = false;
                    cimLog.WriteLog(exError, cimLog.LogType.Warning);
                    ProcessOffline();
                }
            }
        }

        // Do not write anything if we failed the request. We may want to use old data
        // Watch for io errors. The file may be locked
        if (success)
        {
            HttpContext.Current.Application["OfflineCount"] = 0;    // Clear any flags when we were off line 
            String filePath = "";

            /* Begin XML validation */
            // Copy streamHttp to a memory stream to allow seeking when using in XMLTextReader
            streamHttp = copyToMemoryStream(streamHttp);
            if (!xmlIsValid(url, streamHttp, durationSeconds))
            {
                schemReader.Close();
                // Write to log when data is invalid
                cimLog.WriteLog("\tFeed provider for feed: " + url + " returned invalid data.");
                // Break out of function to ensure proxy does not cacher bad data
                return;
            }
            schemReader.Close();
            // Reset position of streamHttp;
            streamHttp.Position = 0;
            try
            {
                filePath = WebConfigurationManager.AppSettings["CachePath"].ToString() + cimHTTP.ConvertUrlToFileName(url);
                FileStream streamCache = new FileStream(filePath, FileMode.Create, FileAccess.Write);
                BinaryWriter binCache = new BinaryWriter(streamCache);
                int b;
                while ((b = streamHttp.ReadByte()) != -1)
                {
                    binCache.Write(Convert.ToByte(b));
                }
                binCache.Close();
                streamHttp.Close();
            }
            catch (IOException ioError)
            {
                cimLog.WriteLog("Could not write to " + filePath + ". " + ioError.Message, cimLog.LogType.Warning);
            }
            catch (Exception exError)
            {
                cimLog.WriteLog(exError.Message);
            }
        }

    }

Open in new window

davidcahanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ted BouskillSenior Software DeveloperCommented:
It looks like the HttpWebRequest hasn't been implemented correctly.

This sample shows the correct way to do it:
http://www.codeproject.com/KB/webservices/HttpWebRequest_Response.aspx
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
davidcahanAuthor Commented:
yea...not only was it not implemented correctly but the timeout had been set to .03 milliseconds.  no wonder it was timing out!!!!

I didn't notice that fact until after my post.  I'll give you the points anyways though.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.