1

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();
Feng Jiang
  • 1,776
  • 19
  • 25

2 Answers2

1

--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.

  • 1
    Thanks! I didn't know all the verbose is stderr, instead of stdout. I just got what I want by redirecting and receiving stderr. Thanks a lot! – Feng Jiang Jan 05 '22 at 15:46
  • BTW. I tried to use HttpClient, but I can't get verbose info through it. Here is my question on that: https://stackoverflow.com/questions/70584691/how-to-get-verbose-info-of-the-http-request-from-httpclient-like-those-from-libc – Feng Jiang Jan 05 '22 at 15:49
0

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();
Feng Jiang
  • 1,776
  • 19
  • 25