encoding issues with content in response from HttpWebRequest

谁说我不能喝 提交于 2019-12-24 00:15:14

问题


I am using a HttpWebRequest to read in a web page using the following code:

 var pageurl = new Uri(url, UriKind.Absolute);

        var request = (HttpWebRequest)WebRequest.Create(pageurl);
        request.Method = "GET";
        request.AutomaticDecompression = DecompressionMethods.GZip;
        request.KeepAlive = false;
        request.ConnectionGroupName = Guid.NewGuid().ToString();
        request.ServicePoint.Expect100Continue = false;
        request.Pipelined = false;
        request.MaximumResponseHeadersLength = 4;

        if (ignoreCertificateErrors)
        {
            ServicePointManager.ServerCertificateValidationCallback += AcceptAllCertificatePolicy;
        }

        var response = (HttpWebResponse)request.GetResponse();


    if (response != null)
        {
            using (var reader = new StreamReader(response.GetResponseStream()))
            {
                return reader.ReadToEnd();
            }
        }

This works perfectly when the language being passed is english but when another language such as spanish then I get numerous � in the returned content.

Is there a problem with the code or is there something encoding wise I am missing?


回答1:


You have to specify the correct encoding for the page you're downloading to StreamReader. For example, if the page is in the encoding ISO-8859-2, use

new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("ISO-8859-2"))


来源:https://stackoverflow.com/questions/6103708/encoding-issues-with-content-in-response-from-httpwebrequest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!