从HttpWebRequest的编码响应与内容的问题 [英] encoding issues with content in response from HttpWebRequest

查看:226
本文介绍了从HttpWebRequest的编码响应与内容的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的HttpWebRequest使用以下code在网页阅读:

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.

是否有与code的一个问题或有什么编码明智我丢失?

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

推荐答案

您必须指定你下载到的StreamReader 页面正确的编码。例如,如果网页是在编码ISO-8859-2,用

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"))

这篇关于从HttpWebRequest的编码响应与内容的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆