Проблемы с отправкой CDATA в PHP с помощью HttpRequest в C # - PullRequest
0 голосов
/ 31 августа 2011

Я пытаюсь загрузить значения из приложения C # в скрипт PHP через POST, но скрипт 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);

в полях данных POST, которые содержат CDATA, но скрипт PHP по-прежнему не обрабатывает & amp;и & quot;должным образом.Например, передача строки

user=a&P&password=lol

даст

user=a
amp;P=
password=lol

вместо намеченного

user=a&P
password=lol

1 Ответ

0 голосов
/ 31 августа 2011

Вы не хотите использовать System.Net.WebUtility.HtmlEncode(content); Попробуйте использовать HttpUtility.UrlEncode вместо

...