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.