0

我有一个网站,其内容是法语。

现在我想使用 c# 在控制台应用程序中HttpWebRequest完成这些。HttpWebResponse

public string GetContents(string url)
{
    StreamReader _Answer;
    try
    {
        HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(url);
        WebReq.Headers.Add(HttpRequestHeader.AcceptEncoding, "utf-8");
        WebReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0;Windows NT 5.1;)";
        WebReq.ContentType = "application/x-www-form-urlencoded";
        HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
        Stream Answer = WebResp.GetResponseStream();
        Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
        _Answer = new StreamReader(Answer, Encoding.UTF8);
        return _Answer.ReadToEnd();
    }
    catch
    {
    }
    return "";
}

我得到了内容,但它包含一些奇怪的符号,如正方形等。

4

1 回答 1

4

Are you sure the web server is responding with UTF-8 encoding?

Update:

The web server from which you are trying to download is serving the pages with a character encoding of ISO-8859-1 and not UTF-8.

You have to (a) change your hard coded content type or (b) read the content type from the server response and use that.

于 2011-07-01T07:24:20.397 回答