473,396 Members | 1,843 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,396 software developers and data experts.

Read bytes from GetResponseStream.

SK
Hi,

I am not able to convert the response stream from HttpWebResponse into bytes
properly.

Here is the relevent code -

HttpWebResponse response = (HttpWebResponse)request.GetResponse ();

// Get the stream associated with the response.
Stream receiveStream = response.GetResponseStream ();

// Pipes the stream to a higher level stream reader with the required
// encoding format.
StreamReader readStream = new StreamReader (receiveStream, Encoding.ASCII);

string receivestream = readStream.ReadToEnd ();
// rgBuffer is a big enough byte array.
rgBuffer = Encoding.ASCII.GetBytes (receivestream) ;

response.Close ();
readStream.Close ();

I see a problem in the way I am converting my response to bytes. rgBuffer
doesn't carry correct values.
What is the correct way?

Thanks,
SK
Nov 15 '05 #1
2 28517
SK <sk@hotmail.com> wrote:
I am not able to convert the response stream from HttpWebResponse into bytes
properly.
The response stream is in bytes already - there's no conversion needed.
The problem is that you're converting it from bytes to text and back in
a lossy way.
Here is the relevent code -

HttpWebResponse response = (HttpWebResponse)request.GetResponse ();

// Get the stream associated with the response.
Stream receiveStream = response.GetResponseStream ();

// Pipes the stream to a higher level stream reader with the required
// encoding format.
StreamReader readStream = new StreamReader (receiveStream, Encoding.ASCII);

string receivestream = readStream.ReadToEnd ();
// rgBuffer is a big enough byte array.
rgBuffer = Encoding.ASCII.GetBytes (receivestream) ;

response.Close ();
readStream.Close ();

I see a problem in the way I am converting my response to bytes. rgBuffer
doesn't carry correct values.
What is the correct way?


You're converting the bytes to ASCII and back again. ASCII only has 128
values. Fundamentally, this is binary data and you have no good reason
to convert it to text and back again.

See http://www.pobox.com/~skeet/csharp/unicode.html
and
http://www.pobox.com/~skeet/csharp/readbinary.html
for more information - the latter has some code called ReadFully which
should give you the binary equivalent of StreamReader.ReadToEnd.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
SK

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
SK <sk@hotmail.com> wrote:
I am not able to convert the response stream from HttpWebResponse into bytes
properly.


The response stream is in bytes already - there's no conversion needed.
The problem is that you're converting it from bytes to text and back in
a lossy way.
Here is the relevent code -

HttpWebResponse response = (HttpWebResponse)request.GetResponse ();

// Get the stream associated with the response.
Stream receiveStream = response.GetResponseStream ();

// Pipes the stream to a higher level stream reader with the required
// encoding format.
StreamReader readStream = new StreamReader (receiveStream, Encoding.ASCII);

string receivestream = readStream.ReadToEnd ();
// rgBuffer is a big enough byte array.
rgBuffer = Encoding.ASCII.GetBytes (receivestream) ;

response.Close ();
readStream.Close ();

I see a problem in the way I am converting my response to bytes. rgBuffer
doesn't carry correct values.
What is the correct way?


You're converting the bytes to ASCII and back again. ASCII only has 128
values. Fundamentally, this is binary data and you have no good reason
to convert it to text and back again.

See http://www.pobox.com/~skeet/csharp/unicode.html
and
http://www.pobox.com/~skeet/csharp/readbinary.html
for more information - the latter has some code called ReadFully which
should give you the binary equivalent of StreamReader.ReadToEnd.

Cool..works after making changes like ReadFully :-)
Thank you,
SK
Nov 15 '05 #3

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

Similar topics

0
by: Koji Ishii | last post by:
According to the doc, Stream.Read() returns: - # of bytes read, or - 0 for EOF But could that be a negative value? If so, how should we program against such case? I have a loop like this:...
1
by: Brian | last post by:
I'm having intermittent trouble with a call to the Read method of the HttpWebResponse object. I get an ArumentOutOfRangeException claiming that deep down inside of the Read method, the count...
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...
5
by: Fernando Rodríguez | last post by:
Hi, How can I read everything inside a Stream into a String or StringBuilder? O:-) TIA
0
by: XML newbie: Urgent pls help! | last post by:
I am using VB.Net. My program is to connect to a remote IPAddress. Once, it verifies the login information it should display the SessionID and enable some button . I appreciate your help and thanku...
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 ...
2
by: =?Utf-8?B?cGVsZWdrMQ==?= | last post by:
when i send an xml to a server using WebRequest object (i am sending a paramater+xml in size of about 250 chars) i recve an error : System.Net.WebException was unhandled Message="The request...
2
by: tovishal2001 | last post by:
Hi, I am trying to read first 6000 bytes from a webpage, using StreamReader.Read(buffer, offset, no_of_bytes_to_read) method and trying to close the connection. Because, the useful data I need is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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,...

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.