HttpWebRequest/Response Exception
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: HttpWebRequest/Response Exception

  1. #1
    Join Date
    Oct 2011
    Posts
    17

    HttpWebRequest/Response Exception

    Hi, Im trying to build an app that retrieves real-time fx quotes.

    Im getting an exception "cannot access a disposed object" when I try to make a 2nd call to GetResponse() even though I am disposing off it through using statements (I've tried to call Close() manually; this did not solve problem).

    Code:
    static void Main(string[] args)
            {
                string sessionID = "xxxxxx";
                
                HttpWebRequest rateRequest = HttpWebRequest.Create(string.Format(@"http://webrates.truefx.com/rates/connect.html?id={0}", sessionID)) as HttpWebRequest;
                HttpWebResponse rateResponse;
        
                    while (true)
                    {
                        using (rateResponse = rateRequest.GetResponse() as HttpWebResponse)
                        {
                            if (rateResponse != null)
                            {
                                 using (StreamReader rateReader = new StreamReader(rateResponse.GetResponseStream()) )
                                 {
                                     Console.WriteLine(rateReader.ReadToEnd());
                                 }
                            }
                        }
                    }
                }

    Would really appreciate some help!
    Last edited by techtalk7; January 8th, 2012 at 04:56 PM.

  2. #2
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: HttpWebRequest/Response Exception

    I believe you need to create a new HttpWebRequest object for each request. You can't recycle an existing one. Try:

    Code:
            static void Main(string[] args)
            {
                string sessionID = "xxxxxx";
    
                while (true)
                {
                    HttpWebRequest rateRequest = HttpWebRequest.Create(string.Format(@"http://webrates.truefx.com/rates/connect.html?id={0}", sessionID)) as HttpWebRequest;
                    HttpWebResponse rateResponse;
    
                    using (rateResponse = rateRequest.GetResponse() as HttpWebResponse)
                    {
                        if (rateResponse != null)
                        {
                            using (StreamReader rateReader = new StreamReader(rateResponse.GetResponseStream()))
                            {
                                Console.WriteLine(rateReader.ReadToEnd());
                            }
                        }
                    }
                }
            }
    I put the HttpWebRequest creation code in the scope of the while loop so it will be recreated each time it is needed. I also tested it on my machine and while I got a message of not authorized from the server, no exceptions were generated
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

  3. #3
    Join Date
    Oct 2011
    Posts
    17

    Re: HttpWebRequest/Response Exception

    This worked thank you!

  4. #4
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: HttpWebRequest/Response Exception

    i should point out that this code is massively inconsiderate and badly behaved because there's no rate control; it just hammers out a new request as soon as the old one finishes. You should consider adding a timer and making only one request per minute, or longer. I you truly do need fx rate updats more frequently than this, the decent thing to do would be to contact truefx and asking them for the most efficient way to receive currency updats rather than hammering the living daylights out of the free service they provide...
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center