2011-08-30 47 views
0

我試圖通過POST將C#應用程序的值上傳到PHP腳本,但是php腳本似乎忽略了轉義實體。在C中使用HttpRequest發送CDATA到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但PHP腳本仍然不處理&放大器POST數據領域

;和&「正常。例如,傳遞字符串

user=a&P&password=lol 

會產生

user=a 
amp;P= 
password=lol 

而不是預期的

user=a&P 
password=lol 

回答