Search code examples
.netwebclientuploadstring

WebClients uploadstring


I'm using the WebClient to post string to a WebApiController using UploadString.

The string Im uploading is an Xml file containing a DateTime in the following format: "yyyy-mm-ddThh:mm:ss.000+zzz".

The problem I encounter is that the string is sent ok but the endpoint ApiController gets the string without the "+" character in the DateTime. It is replaced with " ".

How can I format the Xml string so that I will get the "+" character in the ApiController?

I tried

HttpUtility.UrlEncode(xmlString) 

but it didn't do anything.

Thanks a lot,

Rotem


Solution

  • string someData = "<foo>yyyy-mm-ddThh:mm:ss.000+zzz</foo>";
    string urlPart = HttpUtility.UrlEncode(someData);
    

    Gives:

    %3cfoo%3eyyyy-mm-ddThh%3amm%3ass.000%2bzzz%3c%2ffoo%3e
    

    which should work fine; the %2b is the encoded +. An alternative is:

    string someData = "<foo>yyyy-mm-ddThh:mm:ss.000+zzz</foo>";
    string urlPart = Uri.EscapeDataString(someData);
    

    which gives

    %3Cfoo%3Eyyyy-mm-ddThh%3Amm%3Ass.000%2Bzzz%3C%2Ffoo%3E
    

    which is semantically identical (it only differs in the case of the %-encoded tokens, but that doesn't matter). It should work fine either way.