开发者

How to get content of httpWebresponse in proper string form?

开发者 https://www.devze.com 2023-04-08 04:25 出处:网络
Sometimes I am getting kind of garbled response from several web si开发者_开发问答tes. Here is my code:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消