简体   繁体   English

c# OutputDataReceived 不适用于 curl

[英]c# OutputDataReceived does not work with curl

Following is the code to use OutputDataReceived to capture verbose information from curl.以下是使用 OutputDataReceived 从 curl 捕获详细信息的代码。

There is no information can be captured this way.没有任何信息可以通过这种方式捕获。 What is wrong?怎么了?

var command = "curl.exe -vs -o test.html https:\\www.google.com";
var procStartInfo = new ProcessStartInfo("cmd", "/c " + command);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
var proc = new Process();
proc.EnableRaisingEvents = true;
proc.OutputDataReceived += (s, e) => {if(!string.IsNullOrEmpty(e.Data)) textBoxLog.AppendText(e.Data); };
proc.StartInfo = procStartInfo;
proc.Start();
proc.BeginOutputReadLine();
proc.WaitForExit();
proc.Close();

--output (or -o) writes the downloaded content into the given file (instead of writing it into stdout). --output(或 -o)将下载的内容写入给定文件(而不是将其写入标准输出)。 The rest of cURL's output (progress meter, error messages, verbose mode etc.) is still written into stderr, which is shown in the terminal. cURL 的 output 的 rest(进度表、错误消息、详细模式等)仍然写入 stderr,显示在终端中。 https://en.wikipedia.org/wiki/Standard_streams https://en.wikipedia.org/wiki/Standard_streams

This means that you can only see the output of the HTML in C# with OutputDataReceived but not the output made by the verbose mode. This means that you can only see the output of the HTML in C# with OutputDataReceived but not the output made by the verbose mode.

This code in a running console application, prints all the verbose info to the console without writing it manually with Console.WriteLine() :正在运行的控制台应用程序中的此代码将所有详细信息打印到控制台,而无需使用Console.WriteLine()手动写入:

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = @"C:\Windows\System32\curl.exe";
startInfo.Arguments = @"https://vi.stackexchange.com/ -vs";
startInfo.RedirectStandardOutput = true;
process.StartInfo = startInfo;
process.Start();

You could save the output of the verbose mode to a txt file with curl through the help of a bat in this way:您可以通过蝙蝠的帮助将详细模式的 output 保存到带有curltxt 文件中:

curl https://vi.stackexchange.com/ -vs >curl-output.txt 2>&1

Or you could read the StandardError stream with ErrorDataReceived .或者您可以阅读带有ErrorDataReceivedStandardError stream 。

I would recommend to use the HttpWebRequest as shown on this question instead of using curl to do a request as a Process , unless you have a specific reason.我建议使用HttpWebRequest ,如this question所示,而不是使用 curl 将请求作为Process ,除非您有特定原因。

Thanks Malware Werewolf for pointing me to the right direction.感谢恶意软件狼人为我指明了正确的方向。 The verbose info is stderr, not stdout.详细信息是标准错误,而不是标准输出。 Following is corrected code to capture the verbose info.以下是用于捕获详细信息的更正代码。

var command = "curl.exe -vs -o test.html https:\\www.google.com";
var procStartInfo = new ProcessStartInfo("cmd", "/c " + command);
procStartInfo.RedirectStandardError = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
var proc = new Process();
proc.EnableRaisingEvents = true;
proc.ErrorDataReceived += (s, e) => {if(!string.IsNullOrEmpty(e.Data)) textBoxLog.AppendText(e.Data); };
proc.StartInfo = procStartInfo;
proc.Start();
proc.BeginErrorReadLine();
proc.WaitForExit();
proc.Close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM