Home > OS >  c# OutputDataReceived does not work with curl
c# OutputDataReceived does not work with curl

Time:01-06

Following is the code to use OutputDataReceived to capture verbose information from 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();

CodePudding user response:

--output (or -o) writes the downloaded content into the given file (instead of writing it into stdout). The rest of cURL's output (progress meter, error messages, verbose mode etc.) is still written into stderr, which is shown in the terminal. 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 code in a running console application, prints all the verbose info to the console without writing it manually with 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:

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

Or you could read the StandardError stream with ErrorDataReceived.

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.

CodePudding user response:

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();
  •  Tags:  
  • Related