Link to home
Start Free TrialLog in
Avatar of davidcahan
davidcahanFlag for United States of America

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of Ted Bouskill
Ted Bouskill
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of davidcahan

ASKER

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.