Connecting Tech Pros Worldwide Forums | Help | Site Map

Read from any URL into Stream

Just D.
Guest
 
Posts: n/a
#1: Nov 17 '05
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.



Just D.
Guest
 
Posts: n/a
#2: Nov 17 '05

re: Read from any URL into Stream


Sorry, guys, the question is cancelled. It was easy.

http://groups.google.com/group/micro...f258ee1c2a2a37

This is the fragment from this site:

WebClient webClient = new WebClient();
try
{
webClient.Credentials = CredentialCache.DefaultCredentials;
webClient.DownloadFile(url, fileName);


}


catch (System.Exception e)
{
// Do something with exception

}


finally
{
webClient.Dispose();


}


Jason Newell

Regards,
Just D.


"Just D." <no@spam.please> wrote in message
news:NfI4f.55688$WR2.38149@fed1read03...[color=blue]
> 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.[/color]


Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#3: Nov 17 '05

re: Read from any URL into Stream


Just D. <no@spam.please> wrote:[color=blue]
> 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.[/color]

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

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Closed Thread