简体   繁体   English

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

[英]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. 我正在尝试通过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;
        }
    }

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; 在包含CDATA的POST数据字段中,但PHP脚本仍无法处理& 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); 您不想使用System.Net.WebUtility.HtmlEncode(content); Consider using the HttpUtility.UrlEncode instead 考虑改用HttpUtility.UrlEncode

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM