Encoding... sigh.

C

C# Learner

response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(),
Encoding.Default);
string s = sr.ReadToEnd();

When the above is used to download an image, say, the image's bytes
get messed up. How can I do the above *without* messing with the
bytes?
 
C

Champika Nirosh

I don't think that you can assign the byte stream to a string at any rate..

What you really want to do?

Nirosh.
 
C

C# Learner

Champika Nirosh said:
I don't think that you can assign the byte stream to a string at any rate..

The code I wrote works, but not if there are any bytes in the stream
that are out of the range of whatever encoding is used.
What you really want to do?

I'm trying to get the *raw* bytes of what's in the stream. In effect,
I'm trying to download data over HTTP, *and* be able able to
manipulate the raw bytes.

Regards
 
C

Champika Nirosh

As you said, Yes your code may work but it won't show you the actual content
as you assign the byte stream into string.

For manupulation you can use the stream reader itself, same time it would be
easier if you can load the byte stream to a System.IO.Stream class

Nirosh.
 
J

Jon Skeet [C# MVP]

C# Learner said:
response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(),
Encoding.Default);
string s = sr.ReadToEnd();

When the above is used to download an image, say, the image's bytes
get messed up. How can I do the above *without* messing with the
bytes?

You don't. You don't mix binary data and character data. Strings are
for character data, *not* binary data.

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

Jon Skeet [C# MVP]

C# Learner said:
I'm trying to get the *raw* bytes of what's in the stream.

Then just read the content from the stream, and don't try to convert it
into a string.
 
C

C# Learner

Jon Skeet said:
Then just read the content from the stream, and don't try to convert it
into a string.

But I need to eventually convert this content into an array of bytes
(to be sent off to another socket). I really can't have decoding
happening here. :-(

Thanks.
 
J

Jon Skeet [C# MVP]

C# Learner said:
But I need to eventually convert this content into an array of bytes
(to be sent off to another socket). I really can't have decoding
happening here. :-(

Then why were you trying to read it into a string? Just read it into an
array of bytes. You can use MemoryStream to make that easier for you -
just read a block at a time, writing the contents of the block to the
MemoryStream, and then call ToArray at the end.
 
C

C# Learner

Jon Skeet said:
Then why were you trying to read it into a string? Just read it into an
array of bytes. You can use MemoryStream to make that easier for you -
just read a block at a time, writing the contents of the block to the
MemoryStream, and then call ToArray at the end.

Can you show me an example please? I don't quite understand how I'd
do that.

Thanks
 

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