如何在c#上的web请求中指定内容字符集?

时间:2012-04-19 23:17:05

标签: c# character-encoding httpwebrequest

这是一个向远程网站发出请求的函数代码:

private static string translatePage(string text, string langPair, Encoding encoding) {
    string urlBabelfish = "http://babelfish.yahoo.com/translate_txt";
    string urlReverso = "http://www.reverso.net/text_translation.aspx?lang=RU#";
    string url = "";

    // Create a request using a URL that can receive a post. 
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(urlBabelfish);

    // Set the Method property of the request to POST.
    request.Method = "POST";

    // Create POST data and convert it to a byte array.
    string postData = string.Format("lp={0}&trtext={1}", langPair, text);
    byte[] byteArray = encoding.GetBytes(postData);

    // Set the ContentType property of the WebRequest.
    request.ContentType = "application/x-www-form-urlencoded";
    System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType(
        request.ContentType);
    ct.CharSet = encoding.ToString();
    request.ContentType = ct.ToString();

    // Set the ContentLength property of the WebRequest.
    request.ContentLength = byteArray.Length;

    // Get the request stream.
    Stream dataStream = request.GetRequestStream();

    // Write the data to the request stream.
    dataStream.Write(byteArray, 0, byteArray.Length);

    // Close the Stream object.
    dataStream.Close();

    // Get the response.
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

    // Display the status.
    Console.WriteLine(((HttpWebResponse)response).StatusDescription);

    // Get the stream containing content returned by the server.
    dataStream = response.GetResponseStream();

    // Open the stream using a StreamReader for easy access.
    string resPage = "";
    using (dataStream)
    {
        using (StreamReader sr = new StreamReader(dataStream, encoding))
            resPage = sr.ReadToEnd();
    }
    response.Close();

    return resPage;
}

使用输入参数langPair="en_ru"调用此函数会返回错误编码的页面,该页面不允许使用cyrilic符号。 ContentType元标记如下所示:

<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">

所有cyrilic符号都变为'\0'

如果我在浏览器中使用相同参数手动执行请求,则会返回带有标记的UTF-8编码的精细页面

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

我希望我的代码也这样做。我将UTF-8作为Encoding参数传递,但它不会影响ContentType元标记。

我可以在代码中做些什么来使请求返回我需要的编码页面?

2 个答案:

答案 0 :(得分:1)

检查response.ContentType。它应该包含charset=参数。您可以使用它来创建在创建Encoding时使用的正确StreamReader

答案 1 :(得分:1)

Jim Mischel的回答帮助了我。

如果您想知道如何设置ContentType和CharSet,请按照以下步骤操作:

var request = new HttpRequestMessage(HttpMethod.Post, "http://yourwebsite.com:80/Api/")
{
    Content = new StringContent(messageBodyAsString)
};
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json")
{
    CharSet = "utf-8"
};

然后,请求会发送Content-Type,其值为application/json; charset=utf-8