1

I call CURL in the C# to retrieve data.

The following is my code:

Process p = new Process();
p.StartInfo.WorkingDirectory = @"D:\";
p.StartInfo.FileName = @"D:\curl.exe";
p.StartInfo.CreateNoWindow = true;
p.StartInfo.Arguments = "-u agentemail:password -k https://fantasia.zendesk.com/api/v1/tickets/1.xml";
p.Start();
p.WaitForExit();

But the problem is after CURL get the URL data, how do I retrieve the data from the CURL? Is there a command to pull the data into a string like the following?

string x = p.OutputDataReceived();

or

p.OutputDataReceived(string x);

Thank you very much.

DragonZelda
  • 168
  • 1
  • 2
  • 12
  • possible duplicate of [Capturing nslookup shell output with C#](http://stackoverflow.com/questions/353601/capturing-nslookup-shell-output-with-c-sharp) – D'Arcy Rittich Mar 15 '12 at 11:04
  • Have you considered using [WebClient](http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx) or [HttpWebRequest](http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx) instead of executing an external process to make HTTP requests? – dtb Mar 15 '12 at 11:06
  • possible duplicate of [Read response from CURL.exe program](http://stackoverflow.com/questions/9706162/read-response-from-curl-exe-program) – dtb Mar 15 '12 at 11:06

2 Answers2

2
ProcessStartInfo start = new ProcessStartInfo();

start.FileName = @"C:\curl.exe";  // Specify exe name.

start.Arguments = "-i -X POST -H \"Content-Type: text/xml\" -u \"curl:D6i\" --insecure --data-binary @" + cestaXmlUsers + " \"https://xxx.sk/users-import\"";

start.UseShellExecute = false;

start.RedirectStandardOutput = true;

Process p = Process.Start(start);

string result = p.StandardOutput.ReadToEnd();

p.WaitForExit();
Rizier123
  • 58,877
  • 16
  • 101
  • 156
Peter
  • 21
  • 2
1

You could add these lines:

ProcessStartInfo start = new ProcessStartInfo();
start.FileName = @"D:\curl.exe";  // Specify exe name.
start.Arguments = "-u agentemail:password -k https://fantasia.zendesk.com/api/v1/tickets/1.xml";
start.UseShellExecute = false;
start.RedirectStandardOutput = true;

// Start the process.
using (Process p = Process.Start(start)) {
    // Read in all the text from the process with the StreamReader
    using (StreamReader reader = p.StandardOutput) {
        string result = reader.ReadToEnd();
        Console.Write(result);
    }
}
Perception
  • 79,279
  • 19
  • 185
  • 195