在 C# 中使用 HttpRequest 将 CDATA 发送到 PHP 时出现问题
我正在尝试通过 POST 将值从 C# 应用程序上传到 PHP 脚本,但 php 脚本似乎忽略了转义实体。
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.GetResponseStream();
return Answer;
}
catch (WebException ex)
{
MessageBox.Show(ex.ToString());
return null;
}
}
我调用
System.Net.WebUtility.HtmlEncode(content);
包含 CDATA 的 POST 数据字段,但 PHP 脚本仍然不处理 & 。放大器;和& ”适当地。例如,传递字符串
user=a&P&password=lol
会产生
user=a
amp;P=
password=lol
而不是预期的结果
user=a&P
password=lol
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.GetResponseStream();
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不想使用 System.Net.WebUtility.HtmlEncode(content); 考虑使用 HttpUtility.UrlEncode 代替
Yout don't want to use
System.Net.WebUtility.HtmlEncode(content);
Consider using the HttpUtility.UrlEncode instead