从URL获取HTML - StreamReader使用另一种字符编码?

问题描述 投票:1回答:1

我想从这个URL获取HTML:https://store.steampowered.com/app/513710/SCUM/

这应该很简单,但由于SSL / TLS错误,我无法做到这一点。

所以我使用了这个问题的代码:Requesting html over https with c# Webclient

最后我可以填充我的StreamReader,但是当我尝试使用带字符串的ReadToEnd()时,我得到一个损坏的字符串,如下所示:“ ”

这必须是关于字符编码的东西,但如果你打开:https://store.steampowered.com/app/513710/SCUM/

然后打开浏览器控制台,您可以在开头看到:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

在提供的代码处:

webClient.Headers["Accept-Charset"] = "ISO-8859-1,utf-8;q=0.7,*;q=0.7";

你有utf-8,所以我不知道为什么我遇到这个问题。我试图替换:

StreamReader(webClient.OpenRead(steamURL));

附:

StreamReader(webClient.OpenRead(steamURL), Encoding.UTF8, true);

但它仍然没有得到正确的文字。我试图添加所有可能的信息,如果你还需要其他任何我会编辑的问题。

谢谢你的时间,祝你有个愉快的一天。

问候,

大卫

PS:这是我现在的代码:

private StreamReader getStreamReader(string steamURL, WebClient webClient)
{
    return new StreamReader(webClient.OpenRead(steamURL), Encoding.UTF8, true);
}

private void getSteamCosts()
{
    // When I try to access an Steam HTML, SSL error appears
    // We need an specific security protocol
    // I check all, just in case
    ServicePointManager.ServerCertificateValidationCallback =
        new RemoteCertificateValidationCallback(
            delegate
        {
            return true;
        });
    using (WebClient webClient = new WebClient())
    {
        webClient.Headers["User-Agent"] = "Mozilla/5.0 (Windows;"
            + " U; Windows NT 6.0; en-US; rv:1.9.2.6) Gecko/20100625"
            + " Firefox/3.6.6 (.NET CLR 3.5.30729)";
        webClient.Headers["Accept"] = "text/html,application/xhtml+"
            + "xml,application/xml;q=0.9,*/*;q=0.8";
        webClient.Headers["Accept-Language"] = "en-us,en;q=0.5";
        webClient.Headers["Accept-Encoding"] = "gzip,deflate";
        webClient.Headers["Accept-Charset"] = "ISO-8859-1,utf-8;q=0.7,*;q=0.7";

        StreamReader sr = null;

        string steamURL = "https://store.steampowered.com/app/513710/SCUM/";

        try
        {
            // This one should work
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            sr = getStreamReader(steamURL, webClient);
            lbFinalSteam.Text = "TLS12Final";
        }
        catch (Exception) // Bad coding practice, just wanted it to work
        {
            // If that's not the case, I try the rest
            try
            {
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
                sr = getStreamReader(steamURL, webClient);
                lbFinalSteam.Text = "TLSFinal";
            }
            catch (Exception)
            {
                try
                {
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
                    sr = getStreamReader(steamURL, webClient);
                    lbFinalSteam.Text = "SSL3Final";
                }
                catch (Exception)
                {
                    try
                    {
                        ServicePointManager.SecurityProtocol =
                            SecurityProtocolType.Tls11;
                        sr = getStreamReader(steamURL, webClient);
                        lbFinalSteam.Text = "TLS11Final";
                    }
                    catch (Exception)
                    {
                        lbFinalSteam.Text = "NoFinal";
                    }
                }
            }
        }

        if (sr != null)
        {
            string allLines = sr.ReadToEnd();
        }
    }
}

编辑:也许问题是我如何将StreamReader转换为字符串?我的意思是这一行:

string allLines = sr.ReadToEnd();

我应该用别的吗?

c# utf-8 streamreader
1个回答
1
投票

正如https://stackoverflow.com/users/246342/alex-k已经写过的,问题不在于编码,而是我得到了压缩的Gzimp。我刚删除了这个:

webClient.Headers["Accept-Encoding"] = "gzip,deflate";

它的工作原理!谢谢Alex K! :d

© www.soinside.com 2019 - 2024. All rights reserved.