抓取某网站信息时遇到的问题及解决 The character set provided in ContentType is invalid. Cannot read content as string using an invalid character set

var response = httpClient.SendAsync(requestMessage).Result;
content = response.Content.ReadAsStringAsync().Result;

正常是可以获取数据的,不过这次抛了异常,异常信息如下

The character set provided in ContentType is invalid. Cannot read content as string using an invalid character set

解决办法:

代码改为

 var isoContent = "";
  using (var sr = new StreamReader(response.Content.ReadAsStreamAsync().Result, Encoding.GetEncoding("iso-8859-1")))
  {
      isoContent = sr.ReadToEnd();
  }

问题解决

posted @ 2019-04-27 17:17  向萧  阅读(1766)  评论(1编辑  收藏  举报