抓取某网站信息时遇到的问题及解决 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(); }
问题解决