Re: WebRequest problems

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Michael Kremser (moc.NOTUSEABLE.unyd.taerg_at_nuj4002pxe)
Date: 06/04/04


Date: Fri, 04 Jun 2004 15:51:46 +0200


Jon Skeet [C# MVP] wrote:

Hi Jon,

Thank you for your answer!

>>I can't see it's implementing IDisposable at all, at least there's no
>>method "Dispose" also in the "big" fx.
>
>
> There's WebResponse.IDisposable.Dispose - it's done by explicit
> interface implementation. If you look at "about WebResponse class"
> you'll see it implements IDisposable.

Oh yes... I must have overseen this before. ;-)

> And are you always executing that Close()? Don't forget that the reader
> could be throwing an exception.

Well, it hasn't thrown an exception yet, but you're true... it would be
quite better to close it in a finally-statement, but first of all I want
to see it working basically, then I do these tasks. ;-)

> You don't need to call reader.Close(),
> by the way - the using statement will call Dispose for you.

Ah I see. I just wanted to be sure. ;-)

> I don't know whether doing things asynchronously will help - I believe
> the problem is that there's a connection pool and if the responses
> aren't being closed properly, those connections are all in use.

Yes, it seems like that, it must be an error either in the CF or in the
operating system which is not in our (programmers) area of
responsibility. Now, I tried it this way and made a little class
WebDownloader (attached as file [1]), I call it this way:

WebDownloader wd = new WebDownloader(strURL);
wd.Download();
strResult = wd.Response;
GC.Collect(); <-- I thought as it works after restarting the app, it's
may be a programm with handles or something of that type

This works two times serially... then always throws an time out
exception (I have the feeling I already had something that way).

So, it seems to be impossible to make a download three times while the
application is running. Too bad, IMO. IE on the PDA can call a URL as
many times as it likes, so why can't the CF too?

Best regards,

Michael *confused*

[1] Download: http://www.mkcs.at/test/webdownloader.cs

-- 
How to contact me
~~~~~~~~~~~~~~~~~
directly via mail: remove the "NOTUSEABLE" and the point after it and 
reverse the characters
via my portal: go to 
http://great.dynu.com/tools/contact.4s?lang=de&ref=usenet

public class WebDownloader
{
        public WebDownloader(string URL)
        {
                req = (HttpWebRequest)HttpWebRequest.Create(URL);
        }

        public void Download()
        {
                bFinished = false;
                req.BeginGetResponse(new AsyncCallback(ResponseReceived),null);
                while (!bFinished)
                        System.Threading.Thread.Sleep(500);
        }

        private void ResponseReceived(IAsyncResult res)
        {
                try
                {
                        m_resp = (HttpWebResponse)req.EndGetResponse(res);
                }
                catch (Exception ex)
                {
                        MessageBox.Show(ex.ToString(),"ResponseReceived: Error");
                        bFinished = true;
                        return;
                }
                databuffer = new byte[DataBlockSize];
                m_resp.GetResponseStream().BeginRead(databuffer,0,DataBlockSize,new AsyncCallback(OnDataRead),this);
        }

        private void OnDataRead(IAsyncResult res)
        {
                int nBytes = m_resp.GetResponseStream().EndRead(res);
                Response += System.Text.Encoding.UTF7.GetString(databuffer,0,nBytes);
                if (nBytes>0)
                {
                        m_resp.GetResponseStream().BeginRead(databuffer,0,DataBlockSize,new AsyncCallback(OnDataRead),this);
                }
                else
                {
                        // finished
                        bFinished = true;
                }
        }

        private HttpWebResponse m_resp;
        private HttpWebRequest req;
        private byte[] databuffer;
        public string Response = string.Empty;
        private int DataBlockSize = 256;
        private bool bFinished;
}



Relevant Pages

  • Re: Ridicilous!
    ... Jon Skeet [C# MVP] wrote: ... > what resources need to be loaded. ... > the performance hit later on if you get an exception for some other ...
    (microsoft.public.dotnet.framework)
  • Re: Ridicilous!
    ... Jon Skeet [C# MVP] wrote: ... > what resources need to be loaded. ... > the performance hit later on if you get an exception for some other ...
    (microsoft.public.dotnet.general)
  • Re: Date validation
    ... >> exception an spend a lot of time. ... Jon Skeet - ... If replying to the group, please do not mail me too ... Prev by Date: ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Ridicilous!
    ... Jon Skeet [C# MVP] wrote: ... > what resources need to be loaded. ... > the performance hit later on if you get an exception for some other ...
    (microsoft.public.dotnet.languages.csharp)