Keyvan Nayyeri

God breathing through me

Use UTF-8 Encoding in HTTP Requests

Making HTTP requests with common verbs like GET, POST or PUT is a common scenario in today’s development especially web development. In the .NET Framework there is a rich set of API available in System.Net that assist you in dealing with such scenarios.

However, there are some cases when you want to pass content in different languages to the third party server. In such cases, you’ll notice that these classes can not pass the appropriate data to the server. There is a simple tip to work around this that I’m going to present in this short post.

This simple tip (and the simplest way to accomplish this) is passing an extra character set parameter with your content type in your requests. There is a ContentType property for your request objects that can be set to a string value and you usually set it to the MIME type of your request parameters. You can also pass extra parameters to this property as a semi-colon separated list, so you can pass a charset=utf-8 parameter.

The following piece of code gets a URI and a set of parameters to POST onto the third party server and uses this technique to include international characters in its content.

public string PostRequest(Uri url, string formParameters)

{

    ServicePointManager.Expect100Continue = false;

    HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

 

    request.UserAgent = "Nayyeri.NET Samples";

    request.Timeout = 5000;

    request.Method = "POST";

    request.ContentLength = formParameters.Length;

    request.ContentType = "application/x-www-form-urlencoded; charset=utf-8";

    request.KeepAlive = true;

 

    using (var myWriter = new StreamWriter(request.GetRequestStream()))

    {

        myWriter.Write(formParameters);

    }

 

    var response = (HttpWebResponse)request.GetResponse();

    if (response.StatusCode < HttpStatusCode.OK &&

        response.StatusCode >= HttpStatusCode.Ambiguous)

        throw new Exception(response.StatusCode.ToString());

 

    string responseText;

    using (var reader = new StreamReader(response.GetResponseStream(),

        Encoding.ASCII))

    {

        responseText = reader.ReadToEnd();

    }

 

    return responseText;

}

That’s all you need to do! It’s worthwhile to always set this in your code regardless of being sure about your content or not.

3 Comments

Marc Brooks
Sep 30, 2008 8:16 PM
#

Why are you calling string.Format when throwing the exception? There's NO formatting parameters given!

Exception(string.Format(response.StatusCode.ToString()));

should be:

Exception(response.StatusCode.ToString());


Keyvan Nayyeri
Sep 30, 2008 10:27 PM
#

@Marc

Yes, you're right. Actually I was going to add a literal text to describe the exception but completely forgot that :-P

Pingback from Dew Drop – September 30, 2008 (Evening Edition) | Alvin Ashcraft's Morning Dew

Leave a Comment





Ads Powered by Lake Quincy Media Network