1

我正在尝试使用以下代码从 .NET 调用 php 的 HTML 净化器:

    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();

..一切正常,除了...我无法取回非asci字符。例如,在输入中提供一些韩语字符会返回问号作为输出。

即使 HTMLPurifier 函数被绕过并且我只是想简单地提供输入 .NET,将其存储在 php 变量中,然后将该变量回显到输出,也会发生这种情况。

有任何想法吗?

4

2 回答 2

3

感谢您的指点。我确实设法解决了它。问题是为输入和输出显式指定 UTF-8。最后,woking 代码如下所示:

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();
于 2009-02-10T05:21:00.090 回答
2

尝试

myProcess.StartInfo.StandardOutputEncoding = System.Text.Encoding.UTF8;
于 2009-02-10T02:46:46.343 回答