在 C# 中使用 HttpRequest 将 CDATA 发送到 PHP 时出现问题

发布于 2024-12-02 08:12:42 字数 1832 浏览 1 评论 0原文

我正在尝试通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半步萧音过轻尘 2024-12-09 08:12:42

您不想使用 System.Net.WebUtility.HtmlEncode(content); 考虑使用 HttpUtility.UrlEncode 代替

Yout don't want to use System.Net.WebUtility.HtmlEncode(content); Consider using the HttpUtility.UrlEncode instead

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文