You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ranorex调用Newman命令行输出乱码,无法显示正常统计信息

解决C# Process读取Newman命令行输出乱码问题

我在Ranorex用户代码中用C#的Process类启动命令行执行Newman请求,执行过程正常,但读取命令行输出时大量乱码,偶尔才有可读内容,看不到Newman原本应该输出的规整统计信息。尝试切换为UTF-8编码后问题依旧。

我的代码如下:

lccargscomplete = "run \"" + lccargs + "\" --insecure --folder \"" + requestName + "\"";

Report.Log(ReportLevel.Info, "LCCpath: " + lccpath);
Report.Log(ReportLevel.Info, "LCCargs: " + lccargscomplete);
Report.Log(ReportLevel.Info, "Expected: " + returnCode);

Process process = new Process();
process.StartInfo.FileName = lccpath;
process.StartInfo.Arguments = lccargscomplete;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardInput = true;
process.EnableRaisingEvents = true;

process.OutputDataReceived += OutputDataReceived;
process.ErrorDataReceived += ErrorDataReceived;

process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();

解决方法

乱码核心原因是Process读取的编码和Newman/命令行输出的编码不匹配,Windows命令行默认用GBK(OEM编码)而非UTF-8,直接设UTF-8自然会乱码。按以下步骤修改:

  1. 注册编码提供器(针对.NET Core/.NET 5+)
    如果你的项目是.NET Core或更高版本,需要先注册编码提供器才能访问GBK这类非内置编码:

    System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
    
  2. 设置Process的输出编码
    在初始化Process.StartInfo时,添加编码设置,匹配命令行的默认编码:

    // 用GBK编码(对应中文Windows命令行的默认编码)
    process.StartInfo.StandardOutputEncoding = System.Text.Encoding.GetEncoding("GBK");
    process.StartInfo.StandardErrorEncoding = System.Text.Encoding.GetEncoding("GBK");
    // 或者直接用控制台当前编码,适配不同系统环境
    // process.StartInfo.StandardOutputEncoding = Console.OutputEncoding;
    // process.StartInfo.StandardErrorEncoding = Console.ErrorEncoding;
    
  3. 修改后的完整代码示例

    lccargscomplete = "run \"" + lccargs + "\" --insecure --folder \"" + requestName + "\"";
    
    Report.Log(ReportLevel.Info, "LCCpath: " + lccpath);
    Report.Log(ReportLevel.Info, "LCCargs: " + lccargscomplete);
    Report.Log(ReportLevel.Info, "Expected: " + returnCode);
    
    // 注册编码提供器(.NET Core/.NET 5+需要)
    System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
    
    Process process = new Process();
    process.StartInfo.FileName = lccpath;
    process.StartInfo.Arguments = lccargscomplete;
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.RedirectStandardError = true;
    process.StartInfo.RedirectStandardInput = true;
    process.EnableRaisingEvents = true;
    
    // 设置匹配的编码
    process.StartInfo.StandardOutputEncoding = System.Text.Encoding.GetEncoding("GBK");
    process.StartInfo.StandardErrorEncoding = System.Text.Encoding.GetEncoding("GBK");
    
    process.OutputDataReceived += OutputDataReceived;
    process.ErrorDataReceived += ErrorDataReceived;
    
    process.Start();
    process.BeginOutputReadLine();
    process.BeginErrorReadLine();
    process.WaitForExit();
    

补充说明

如果设置GBK后仍有问题,可以尝试在启动命令前添加chcp 65001 && 强制切换到UTF-8编码再执行Newman命令,但这种方式不如直接设置Process编码稳定。

内容的提问来源于stack exchange,提问作者Jasmin Nagel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 23:33:11