Problems Sending CDATA to PHP With HttpRequest in C#
I'm trying to upload values from a C# application to a PHP script via POST, but the php script seems to be ignoring escaped entities.
public static Stream GetStream(string postData, string file)
{
try
{
HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create(remoteServer + "/" + file + "?");
//ASCIIEncoding encoding = new ASCIIEncoding();
UTF8Encoding encoding = new UTF8Encoding();
byte[] data = encoding.GetBytes(postData);
HttpWReq.Method = "POST";
HttpWReq.ContentType = "application/x-www-form-urlencoded";
HttpWReq.ContentLength = data.Length;
HttpWReq.AllowAutoRedirect = false;
Stream newStream = HttpWReq.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
//Get the response handle, we have no true response yet!
HttpWebResponse WebResp = (HttpWebResponse)HttpWReq.GetResponse();
//Let's show some information about the response
Console.WriteLine(WebResp.StatusCode);
Console.WriteLine(WebResp.Server);
Console.WriteLine(WebResp.ResponseUri.ToString());
//Now, we read the response (the string), and output it.
Stream Answer = WebResp.G开发者_运维技巧etResponseStream();
return Answer;
}
catch (WebException ex)
{
MessageBox.Show(ex.ToString());
return null;
}
}
I call
System.Net.WebUtility.HtmlEncode(content);
on the fields of the POST data that contain CDATA but the PHP script still does not handle the & amp; and & quot; properly. For example, passing the string
user=a&P&password=lol
would produce
user=a
amp;P=
password=lol
instead of the intended
user=a&P
password=lol
Yout don't want to use System.Net.WebUtility.HtmlEncode(content);
Consider using the HttpUtility.UrlEncode instead
精彩评论