从 .NET 启动进程但 RedirectedStandardOutput 不支持 UTF-8

发布于 2024-07-13 14:03:45 字数 918 浏览 6 评论 0原文

我正在尝试使用以下代码从.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 变量中,并将该变量回显回输出,也会发生这种情况。

有任何想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

樱花落人离去 2024-07-20 14:03:45

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

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

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();
少年亿悲伤 2024-07-20 14:03:45

尝试

myProcess.StartInfo.StandardOutputEncoding = System.Text.Encoding.UTF8;

Try

myProcess.StartInfo.StandardOutputEncoding = System.Text.Encoding.UTF8;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文