如何使用C#下载编码正确的HTML页面?

如何使用C#下载编码正确的HTML页面?,c#,encoding,screen-scraping,C#,Encoding,Screen Scraping,我需要一个方法来获取一个页面的链接并返回该页面的标题 我使用了网络客户端- var webClient = new WebClient(); var htmlString = webClient.DownloadString(_link); 它工作得很好,但在外语编码方面失败了。我得到的是问号和奇怪的字符,而不是我需要的文本 是否有一种通用的方法来识别页面的编码并使用它?我需要它来支持大多数编码,如果不是全部的话 使用,您可以执行以下操作 using Syste

我需要一个方法来获取一个页面的链接并返回该页面的标题

我使用了网络客户端-

        var webClient = new WebClient();
        var htmlString = webClient.DownloadString(_link);
它工作得很好,但在外语编码方面失败了。我得到的是问号和奇怪的字符,而不是我需要的文本

是否有一种通用的方法来识别页面的编码并使用它?我需要它来支持大多数编码,如果不是全部的话

使用,您可以执行以下操作

using System;
using System.IO;
using System.Net;
using System.Text;

...

    public static void GetFile
            (
            string strURL,
            string strFilePath
            )
        {

            WebRequest myWebRequest = WebRequest.Create(strURL); 

            WebResponse myWebResponse = myWebRequest.GetResponse(); 

            Stream ReceiveStream = myWebResponse.GetResponseStream();

            Encoding encode = System.Text.Encoding.GetEncoding("utf-8");

            StreamReader readStream = new StreamReader( ReceiveStream, encode );

            string strResponse=readStream.ReadToEnd();

            StreamWriter oSw=new StreamWriter(strFilePath);

            oSw.WriteLine(strResponse);

            oSw.Close();

            readStream.Close();

            myWebResponse.Close();

        }
using (WebClient client = new WebClient())
using (var read = client.OpenRead("http://your.com"))
{
    HtmlDocument doc = new HtmlDocument();
    doc.Load(read, true); // true = get encoding from byte order masks
    // process doc, extract title
    var title = doc.DocumentNode.SelectSingleNode("//title").InnerText;
}

网页和/或服务器是否指定了它的编码方式?网页可以是internet上的任何网页,这就是问题:)这显然只适用于页面确实使用UTF-8的情况!此外,它不解码html字符,如ü;