purvang
purvang

Reputation: 339

How to get content of httpWebresponse in proper string form?

Sometimes I am getting kind of garbled response from several web sites.

Here is my code:

Stream responseStream = response.GetResponseStream();
buffer = new Byte[256];//
int bytesRead;
while ((bytesRead = responseStream.Read(buffer, 0, buffer.Length)) > 0)
{
   outStream.Write(buffer, 0, bytesRead);
   //resp=resp+ .UTF8.GetString(buffer, 0, bytesRead);
   resp=resp + Encoding.ASCII.GetString(buffer); //resp is string
}

when I request from www.google.co.in I get following characters in resp string:

?\b\0\0\0\0\0??}y?F?????????Z??????{7m???oX?\r?Y???33??d;y????n?0?

How should I overcome this problem? Is it related to encoding?

Upvotes: 0

Views: 23648

Answers (2)

purvang
purvang

Reputation: 339

The response I received was GZip-compressed, so I just decompressed the response stream as shown below:

Stream responseStream = response.GetResponseStream();
responseStream = new GZipStream(responseStream, CompressionMode.Decompress);

now one can read the stream using the code I provided above.

@Kalyan Thanks for your help!!!

Upvotes: 6

Refer to How to use the GetResponseStream method in C# and also Usage of HttpWebResponse and HttpWebRequest for getting an idea about reading contents from HttpWebResponse. Hope it will help you.

Upvotes: 3

Related Questions