Weava
Weava

Reputation: 496

When sending XML by POST, the symbol & is escaping entire rest of string

I am trying to pass a string formatted as XML to a Web Api controller, and when it is sent, it only receives the string up to the first & symbol, and then cuts off. Is there any way to make sure the & symbols will not escape the string?

Here is an example of my request:

string result = "";
using (var client = new WebClient())
{
     client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";

     string allLines = "=" + param.ToString();
     result = client.UploadString(url, "POST", allLines);
}   
return result;

Upvotes: 2

Views: 2353

Answers (2)

Andrei
Andrei

Reputation: 44640

HTTP header sometimes is not just key-value pair. It can be an array of values divided by & character.

Try to use HttpUtility.UrlEncode(value) when sending value and HttpUtility.UrlDecode(value) when receiving.

Upvotes: 2

Ken Palmer
Ken Palmer

Reputation: 2445

Try Uri.EscapeUriString or HttpUtility.UrlPathEncode. Alternately, you can manually encode an ampersand by replacing it with %26. For instance:

myString.Replace("&", "%26");

Upvotes: 1

Related Questions