473,405 Members | 2,141 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Read from any URL into Stream

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.
Nov 17 '05 #1
2 12273
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...
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.

Nov 17 '05 #2
Just D. <no@spam.please> wrote:
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

--
Jon Skeet - <sk***@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
Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Leon Jollans | last post by:
Hi. I'm reading a 5 meg (or so) file from the response stream of a WebRequest, and it's pitifully slow. I mean *really* slow, in the order of 15 minutes to download. Now obviously I'm caching this...
6
by: Yechezkal Gutfreund | last post by:
I have been using the following code (successfully) to read Xml formated text packets from a TCP stream. The output from the server stream consists of a sequence of well formed Xml documents...
18
by: JG | last post by:
Does anyone know a standard (or supported on Linux, Mac, Win32) way to clear a read stream buffer (standard ANSI C file stream)? I would even settle for a platform specific way of doing it. ...
0
by: Enosh Chang | last post by:
Hi, I encounter one problem about System.IO. I have a binary file that contains following structure, struct MyFormat { public sbyte Type; public sbyte Num; public sbyte Flag;
1
by: Iwan Petrow | last post by:
Hi, I want to create a stream which reads a file bit by bit (the stream has method ReadBit()). Does anybody know some links, articles and examples on this topic. Thanks.
3
by: frustrated | last post by:
I am trying to share a file stream between two threads, but havent got a clue as to how to do it. The first thread will be reading the file, and the second thread will(/might) be writing to the...
9
by: coconet | last post by:
I am trying to use a StreamReader to read consecutive bytes into a byte array until a ";" is hit, then store everything read up until that point into a new byte array. I have a...
1
by: Morten.Snedker | last post by:
How do I use a StreamReader more than once? Dim rs As New StreamReader(fd.FileName, Encoding.Default) Do While rs.Peek() >= 0 ... Loop 'Loop 2 Do While rs.Peek() >= 0 ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.