c#中使用HttpRequest向PHP发送CDATA的问题


Problems Sending CDATA to PHP With HttpRequest in C#

我试图通过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

如果您不想使用System.Net.WebUtility.HtmlEncode(content);,请考虑使用httutility。