473,466 Members | 1,565 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Read everything in a Stream into a string

Hi,

How can I read everything inside a Stream into a String or StringBuilder? O:-)

TIA
Nov 16 '05 #1
5 17082
Fernando,

If you know the encoding for the stream, just wrap the stream in a
StreamReader, and call the ReadToEnd. Be careful if you are reading from a
network stream, as this will cause it to hang if the stream is not
disconnected when done (as opposed to reading messages from the stream and
keeping it open).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Fernando Rodríguez" <fe*******************@fernando-rodriguez.com> wrote in
message news:re********************************@4ax.com...
Hi,

How can I read everything inside a Stream into a String or StringBuilder?
O:-)

TIA

Nov 16 '05 #2
Does the Stream object represent just a character stream? In this case, you
can wrap it in a StreamReader and use its ReadToEnd method to read the whole
thing at once. This method returns a string.

"Fernando Rodríguez" <fe*******************@fernando-rodriguez.com> wrote in
message news:re********************************@4ax.com...
Hi,

How can I read everything inside a Stream into a String or StringBuilder? O:-)
TIA

Nov 16 '05 #3
On Mon, 18 Oct 2004 13:24:14 -0400, "Nicholas Paldino [.NET/C# MVP]"
<mv*@spam.guard.caspershouse.com> wrote:
Fernando,

If you know the encoding for the stream, just wrap the stream in a
StreamReader, and call the ReadToEnd. Be careful if you are reading from a
network stream, as this will cause it to hang if the stream is not
disconnected when done (as opposed to reading messages from the stream and
keeping it open).


Actually it's the return value of HttpWebResponse.GetResponseStream().

I'm afraid that what I need is a better understanding of Streams. Could you
point out some tutorial that cover streams and specifically network streams?
O:-)

TIA
Nov 16 '05 #4
On Mon, 18 Oct 2004 13:26:43 -0400, "Marina" <so*****@nospam.com> wrote:
Does the Stream object represent just a character stream? In this case, you
It's an httpresponse for a web page, so yes, I guess it can only be text.
can wrap it in a StreamReader and use its ReadToEnd method to read the whole
thing at once. This method returns a string.


OK :-)
Nov 16 '05 #5
Fernando,

You should be alright if you are using GetResponseStream. It should
allow you to call ReadToEnd on the stream and get the result.

If you were using a NetworkStream though, it's different, as you can
send say, 32 over the wire (a space), but still have a connection, so the
stream is not complete yet. If you called ReadToEnd on that, then it would
wait for all of the bytes, and then for the stream to close, and therefore,
block.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Fernando Rodríguez" <fe*******************@fernando-rodriguez.com> wrote in
message news:04********************************@4ax.com...
On Mon, 18 Oct 2004 13:24:14 -0400, "Nicholas Paldino [.NET/C# MVP]"
<mv*@spam.guard.caspershouse.com> wrote:
Fernando,

If you know the encoding for the stream, just wrap the stream in a
StreamReader, and call the ReadToEnd. Be careful if you are reading from
a
network stream, as this will cause it to hang if the stream is not
disconnected when done (as opposed to reading messages from the stream and
keeping it open).


Actually it's the return value of HttpWebResponse.GetResponseStream().

I'm afraid that what I need is a better understanding of Streams. Could
you
point out some tutorial that cover streams and specifically network
streams?
O:-)

TIA

Nov 16 '05 #6

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

Similar topics

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...
10
by: Yogi_Bear_79 | last post by:
pardon my ignorance as I am a self-taught hobbyist programmer. I am curious after reading up on SharpZipLib. Can I embed a zipped txt file in my program? Then either read from within the zip...
2
by: Just D. | last post by:
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...
1
by: eiji | last post by:
Hi folks, I'm new to binary-file handling and try to work/learn playing around with MD5. Now everything works fine with small files, but when files get bigger(e.g. 28MB) this loop stops in the...
3
by: computerwolf8 | last post by:
I have a file where I know the lines go as follows: string long string int int string double
14
by: WStoreyII | last post by:
the following code is supposed to read a whole line upto a new line char from a file. however it does not work. it is producing weird results. please help. I had error checking in there for...
2
by: Jack | last post by:
Hi, I want to read a string a chars from a stream, and put it into a string. At the moment, I'm creating a buffer of a fixed size, and reading the stream of text into it. It works, but I have...
0
by: vishnu | last post by:
Hi, Am trying to post the data over https and am getting error in httpwebresponse.getResponseStream.Please help me to get rid of this issue. Here is the message from immediate window ...
5
by: dm3281 | last post by:
Hello, I have a text report from a mainframe that I need to parse. The report has about a 2580 byte header that contains binary information (garbage for the most part); although there are a...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.