从URL获取HTML-StreamReader使用其他字符编码?

时间:2018-09-01 13:02:18

标签: c# utf-8 streamreader

我想从以下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();
        }
    }
}

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

string allLines = sr.ReadToEnd();

我还应该使用其他东西吗?

1 个答案:

答案 0 :(得分:1)

正如https://stackoverflow.com/users/246342/alex-k所写,问题不是编码,而是我得到了压缩的Gzimp。我刚刚删除了此内容:

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

它有效!谢谢Alex K! :D