views:

283

answers:

2

I am trying to call php's HTML purifier from .NET using this code:

    Process myProcess = new Process();

    myProcess.StartInfo.FileName = "C:\Path\to\php.exe";
    myProcess.StartInfo.Arguments = "C:\Path\to\purify.php";
    myProcess.StartInfo.UseShellExecute = false;
    myProcess.StartInfo.RedirectStandardOutput = true;
    myProcess.StartInfo.RedirectStandardInput = true;

    myProcess.Start();

    StreamWriter myStreamWriter = myProcess.StandardInput;

    String inputText;

    inputText = txtCodes.Text;
    if (inputText.Length > 0)
    {
        myStreamWriter.Write(inputText);
    }
   myStreamWriter.Close();

    labMsg.Text = myProcess.StandardOutput.ReadToEnd();

    myProcess.WaitForExit();

    myProcess.Close();

.. and all works fine except ... I am not able to get back non-asci characters. For example providing some Korean characters in the input returns questionmarks as output.

This happens even if the HTMLPurifier function is bypased and I am just trying to simple provide the input .NET, store it in php variable, and echo that variable back to output.

Any ideas?

+2  A: 

Try

myProcess.StartInfo.StandardOutputEncoding = System.Text.Encoding.UTF8;
dreamlax
+2  A: 

Thanks for the pointer. I did actually managed to solve it. The catch was to explicitly specify UTF-8 for BOTH input and Output. In the end the woking code looks like this:

Process myProcess = new Process();

    myProcess.StartInfo.FileName = "C:\Path\to\php.exe";
    myProcess.StartInfo.Arguments = "C:\Path\to\purify.php";

    myProcess.StartInfo.UseShellExecute = false;
    myProcess.StartInfo.RedirectStandardOutput = true;
    myProcess.StartInfo.RedirectStandardInput = true;
    myProcess.StartInfo.StandardOutputEncoding = Encoding.UTF8;

    myProcess.Start();
    StreamWriter myStreamWriter = new StreamWriter(myProcess.StandardInput.BaseStream, Encoding.UTF8);

    String inputText;
    inputText = txtCodes.Text;

    if (inputText.Length > 0)
    {
        myStreamWriter.Write(inputText);
    }

    myStreamWriter.Close();

    labMsg.Text = myProcess.StandardOutput.ReadToEnd();


    myProcess.WaitForExit();

    myProcess.Close();