0

In a C# application, I need to use the curl command. I am using this code:

using (Process myProcess = new Process())
                {
                    int miIntIntervaloActualizacion = GestorConfiguracion.GetIntervaloActualizacion();
                    string miStrUrl = GestorConfiguracion.GetUrl();

                    myProcess.StartInfo.UseShellExecute = false;
                    myProcess.StartInfo.FileName = "curl";
                    myProcess.StartInfo.Arguments = $"--output index.html --url {miStrUrl}";
                    myProcess.StartInfo.CreateNoWindow = false;
                    myProcess.Start();

                    _logger.LogInformation($"IP actualizada en: {DateTime.Now}.");
                    await Task.Delay(miIntIntervaloActualizacion, stoppingToken);
                }

I get the URL from a xml configuration file. This is not the problem.

In the parameter of the command, I have set the output, a html. If I run this command in a command line console, it contains "OK" if the command is successful, but it creates a html that I don't want to create in my application.

So I would like to know if there are some way to get the result of the curl command to know if it is successful or not.

Thanks.

Álvaro García
  • 18,114
  • 30
  • 102
  • 193
  • 6
    Why would you want to use cURL when you have HttpClient? – DavidG Oct 25 '22 at 08:13
  • i guess you want curl if you are looking for platform agnostic code – jmvcollaborator Oct 25 '22 at 08:16
  • 3
    @jmvcollaborator Like HttpClient then? Still no reason to use cURL over plain C# with HttpClient. – DavidG Oct 25 '22 at 08:19
  • Can you define "successful"? – vernou Oct 25 '22 at 08:27
  • I don't think it's a duplicate. The questions are similar, but not identical. As it closed, I will answer in a comment. Sorry for the poor formatting. – vernou Oct 25 '22 at 08:46
  • Add in the curl command the parameter [--fail](https://curl.se/docs/manpage.html#-f). Then curl will exist with a exist with the erreor code 22. You can check the result like `myProcess.WaitForExit(); bool success = myProcess.ExitCode == 0;`. – vernou Oct 25 '22 at 08:46
  • Check also the parameter `--fail-with-body` if you want output the content even in case of failure. – vernou Oct 25 '22 at 08:47
  • I want to use curl because to update the dynamic IP i have to access to the url that they give. If I can use HttpClient as alternative, I can open to this idea. – Álvaro García Oct 25 '22 at 09:28
  • It must be possible... maybe you can open a other question to expose your original problem. – vernou Oct 25 '22 at 09:30
  • I have used HttpClient and it works. Thanks so much. – Álvaro García Oct 25 '22 at 10:24

0 Answers0