سؤال

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

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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.

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top