Search code examples
c#.net-4.0urlencode.net-client-profile

Urlencode large amount of text in .net 4 client C#


What's the best way to urlencode (escape) a large string (50k - 200k characters) in the .net 4 client profile?

System.Net.Uri.EscapeDataString() is limited to 32766 characters.

HttpUtility.UrlEncode is not available in .net 4 client.

The encoded string is to be passed as the value of a parameter in an httprequest as a post.

(Also, is there a .net-4-client profile tag on SO?)


Solution

  • Because a url encoded string is just encoded character by character it means that if you split a string and encode the two parts then you can concatenate them to get the encoded version of the original string.

    So simply loop through and urlencode 30,000 characters at a time and then join all those parts together to get your encoded string.

    I will echo the sentiments of others that you might be better off with a content-type of multipart/form-data. http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4 explains the differences in case you are unaware. Which of these two you choose should make little difference to the destination since both should be fully understood by the target.