2

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;
Andrei
  • 42,814
  • 35
  • 154
  • 218
Weava
  • 496
  • 2
  • 13

2 Answers2

2

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.

Andrei
  • 42,814
  • 35
  • 154
  • 218
1

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

myString.Replace("&", "%26");
Ken Palmer
  • 2,355
  • 5
  • 37
  • 57