Search code examples
c#.nethttpurl-encoding

Encoding my string to send an http request via C#


I am trying to send a simple HTTP request like this:

var client = new WebClient();                
string myString="this is the string i want to send";
message = client.DownloadString("http://www.viralheat.com/api/sentiment/review.xml?text=" + myString + "&api_key="+currentKey);

but some of the strings I send includes # or & or such characters, so I want to encode the string first before sending it, because it throws an error if it includes these special characters without being encoded.


Solution

  • Call Uri.EscapeDataString.
    Unlike HttpUtility, this works on the client profile too.