1

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?)

Charles
  • 50,943
  • 13
  • 104
  • 142
Eugene
  • 10,957
  • 20
  • 69
  • 97
  • 3
    What's the point? Most browsers have a limit on what you can put on the URL that is much lower than that. – Oded Oct 26 '11 at 13:31
  • It's not going to be done in a browser...it's a post to a web api. – Eugene Oct 26 '11 at 13:33
  • 1
    @Oded - He said it's a POST, not a GET. The character limit is specified in the RFC for GET parameters, but there's no limit (beyond technical limitation) on POST data. – Polynomial Oct 26 '11 at 13:33
  • 1
    @Polynomial - Then why URL encode? – Oded Oct 26 '11 at 13:34
  • @Polynomial: There was an edit after the comment clarifying that it was a post request. – Chris Oct 26 '11 at 13:35
  • @Polynomial: Agreed, no need to url encode data if it's not within the URL. Any normal POST request would have a `Content-Length` header a carriage return, then the data. – Brad Christie Oct 26 '11 at 13:36
  • 2
    @Oded - Because the encoding is the same for URLs as it is for POST parameters, e.g. using %20 for space. – Polynomial Oct 26 '11 at 13:37
  • True, you can do that. Still, the standard for non-MIME transfers is to use URL encoding. – Polynomial Oct 26 '11 at 13:40

3 Answers3

0

I would suggest looking in to using a MIME format for posting your data. No need to encode (other than maybe a base64 encoding) and would keep you under the limitation.

Brad Christie
  • 100,477
  • 16
  • 156
  • 200
0

You could manually encode it all using StringBuilder, though it will increase your transfer amount threefold:

string EncodePostData(byte[] data)
{
    var sbData = new StringBuilder();
    foreach(byte b in data)
    {
        sbData.AppendFormat("%{0:x2}", b);
    }
    return sbData.ToString();
}

The standard method, however, is just to supply a MIME type and Content-Length header, then send the data raw.

Polynomial
  • 27,674
  • 12
  • 80
  • 107
0

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.

Chris
  • 27,210
  • 6
  • 71
  • 92
  • I'm aware of the multipart post, but the api doesn't support it. I guess I'll just have to split it up and encode with System.Uri.EscapeDataString(). – Eugene Oct 26 '11 at 14:02