Sindoki
Sindoki

Reputation: 23

How to specify a content character set in web request on c#?

here is a code of a function that makes a request to remote web-site:

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;
}

Calling this function with input parameter langPair="en_ru" returns a page with wrong encoding that doesn't allow cyrilic symbols. The ContentType meta-tag looks like this:

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

And all cyrilic symbols become '\0'.

If I perform the request manually in browser with the same parameters, it returns fine page of UTF-8 encoding with tag

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

I want my code to do the same. I pass UTF-8 as the Encoding parameter, but it does not affect the ContentType metatag.

What can I do in my code to make the request return a page of the encoding I need?

Upvotes: 1

Views: 9567

Answers (2)

Edu
Edu

Reputation: 2643

Jim Mischel's answer helped me.

If you want to know how to set a ContentType and CharSet, this is how to do it:

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"
};

The request will then send Content-Type with the value application/json; charset=utf-8

Upvotes: 1

Jim Mischel
Jim Mischel

Reputation: 133995

Check response.ContentType. It should include a charset= parameter. You can use that to create the proper Encoding to use when creating your StreamReader.

Upvotes: 1

Related Questions