Read from any URL into Stream

J

Just D.

All!

How should we read any file from some URL? I found in MSDN the method
URLDownloadToFile function, but it's for C#. There is another example to
read but it doesn't work if the page is more complicated and includes not
only HTML code but JAVA scripts, etc. Strange, but it doesn't work for me.

// Creates an HttpWebRequest with the specified URL.
HttpWebRequest myHttpWebRequest =
(HttpWebRequest)WebRequest.Create("http://www.somesite.com");
// Sends the HttpWebRequest and waits for the response.
HttpWebResponse myHttpWebResponse =
(HttpWebResponse)myHttpWebRequest.GetResponse();
// Gets the stream associated with the response.
Stream receiveStream = myHttpWebResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
// Pipes the stream to a higher level stream reader with the required
encoding format.
StreamReader readStream = new StreamReader( receiveStream, encode );
long iLength = myHttpWebResponse.ContentLength;

Char[] read = new Char[iLength];
// Reads 256 characters at a time.
int count = readStream.Read( read, 0, (int)iLength );
while (count > 0){
// Dumps the 256 characters on a string and displays the string to the
console.
String str = new String(read, 0, count);
Console.Write(str);
count = readStream.Read(read, 0, (int)iLength);
}//while

Console.WriteLine("");
// Releases the resources of the response.
myHttpWebResponse.Close();
// Releases the resources of the Stream.
readStream.Close();
Console.Read();

The example above works great with some simple sites, but it can't read from
this URL:

http://weather.yahoo.com/forecast/USAZ0034.html

Does anybody know any good method to read any document into MemoryStream or
String? Any example in C# would be nice.

Thanks,
Just D.
 
J

Jon Skeet [C# MVP]

Just D. said:
How should we read any file from some URL? I found in MSDN the method
URLDownloadToFile function, but it's for C#. There is another example to
read but it doesn't work if the page is more complicated and includes not
only HTML code but JAVA scripts, etc. Strange, but it doesn't work for me.

It's always worth saying in what *way* something doesn't work.

Could you repackage your code into a complete program which shows the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top