Sometimes I am getting kind of garbled response from several web si开发者_开发问答tes.
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?
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!!!
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.
精彩评论