How to convert WebResponse.GetResponseStream return into a string?

前端 未结 5 1213
春和景丽
春和景丽 2020-12-05 12:17

I see many examples but all of them read them into byte arrays or 256 chars at a time, slowly. Why?

Is it not advisable to just convert the resulting Stream

5条回答
  •  执念已碎
    2020-12-05 13:07

    Richard Schneider is right. use code below to fetch data from site which is not utf8 charset will get wrong string.

    using (Stream stream = response.GetResponseStream())
    {
       StreamReader reader = new StreamReader(stream, Encoding.UTF8);
       String responseString = reader.ReadToEnd();
    }
    

    " i can't vote.so wrote this.

提交回复
热议问题