Strange problem with streamwriter & webrequest

Tech-Archive recommends: Speed Up your PC by fixing your registry

From: the friendly display name (name_at_discussions.microsoft.com)
Date: 07/27/04


Date: Mon, 26 Jul 2004 21:10:04 -0700

I hope somebody can help me with this, first of all, the code, then the problem:

-------

using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;
using System.Threading;

public class Client
{

        private byte[] buffer;
        private const int size = 1024;

        private Stream responsestream;
        private Uri url;
        private WebRequest request;
        private WebResponse resp;
        private AsyncCallback callback;
        private StreamWriter sw;

        public void maker()
        {
                buffer = new byte[size];

                callback = new AsyncCallback(whencomplete);

                url = new Uri("http://www.epa.gov/epaoswer/hazwaste/recycle/battery.txt");
                request = WebRequest.Create(url);
                resp = request.GetResponse();

                responsestream = resp.GetResponseStream();

                sw = new StreamWriter((new FileStream(@"c:\log2.txt", FileMode.Create)));

                responsestream.BeginRead(buffer, 0, buffer.Length, callback, null);
        }

        void whencomplete (IAsyncResult result)
        {
                int bytesread = responsestream.EndRead(result);

                if (bytesread > 0)
                {

                        string s = Encoding.ASCII.GetString(buffer, 0, bytesread);

                        Console.WriteLine(s);
                        sw.Write(s);

                        responsestream.BeginRead(buffer, 0, size, callback, null);
                }
                sw.Close();
        }

        public static void Main ()
        {
                Client client = new Client();
                client.maker();

                Thread.Sleep(50000);
        }
}

---------

The file i try to download is a ascii text file, and the only reason i have chosen it, was, because google has shown it as one of the first, for my search for text files.

The code is really simple, the problem is:

It hangs. Try to compile this, the first bytes are shown in the console, and are written into the log2.txt file, but, then.. it does nothing, til the thread sleep is over.

If i remove

sw.Write(s) and sw.Close from the whencomplete callback, the whole ascii file is going to be displayed in the console. (unlike before, only few bytes of it are shown, then, it hangs)

And now the interesting part:

if i copy the text file to my local computer, into the inetpub\wwwroot folder (i have iis 5 installed, the OS is windows 2000 professional), activate the iis, and change the path in the maker function for the textfile to http://localhost/battery.txt , the application works fine. It displays all the contents of the text file, while copying to the log2.txt without any problems.

It seems the problem only occurs, with a connection to a internet server, a local http server works with it.

Btw, i have tried to download a other txt file, from a other remote internet server, the same problem.



Relevant Pages

  • IE 7
    ... my internet server is "dial up." ... The slow down load speed is coordinated with lousy down load accuracy. ... You might understand why I hesitate to download IE 7. ...
    (microsoft.public.windowsxp.help_and_support)
  • Re: Aaaarrrrgggghhhh!!! Frustration -- mostly OT
    ... Donna in Idaho ... bucks. ... my Internet server crashes. ... don't download headers/subject lines, ...
    (rec.crafts.textiles.quilting)
  • Re: Aaaarrrrgggghhhh!!! Frustration -- mostly OT
    ... my Internet server crashes. ... don't download headers/subject lines, ... Debra in VA ... See my quilts at ...
    (rec.crafts.textiles.quilting)