0

How can I make the following cURL call via C# console app.

curl -k https://serverName/clearprofile.ashx -H "Host: example.com"

I have tried CurlSharp. Unfortunately I could not build successfully this project because of missing libcurlshim64 assembly.

From here I understood that the best approach is by using HttpClient class, but I don`t know how exactly to make the above mentioned cURL call from my console application.

2
  • 3
    What code? The command line you posted simply runs a GET with a specific header. Have you checked HttpClient's documentation, eg HttpClient.GetStringAsync? Commented Jan 10, 2017 at 11:04
  • My bad, I edited my post. Commented Jan 10, 2017 at 14:36

1 Answer 1

2

If you just want something simple I would use the HttpClient class built into the .Net Framework like this:

using System.Net.Http;
using System.Threading.Tasks;

namespace ScrapCSConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.Host = "example.com";
            Task<string> task = client.GetStringAsync("https://serverName/clearprofile.ashx");
            task.Wait();
            Console.WriteLine(task.Result);
        }
    }
}

For more complex stuff, you can also use the HttpWebRequest class like this:

using System;
using System.IO;
using System.Net;

namespace ScrapCSConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://google.co.uk");

            request.Host = "example.com";

            HttpStatusCode code;
            string responseBody = String.Empty;

            try
            {
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    code = response.StatusCode;
                    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                    {
                        responseBody = reader.ReadToEnd();
                    }
                }
            }
            catch (WebException webEx)
            {
                using (HttpWebResponse response = (HttpWebResponse)webEx.Response)
                {
                    code = response.StatusCode;
                    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                    {
                        responseBody = reader.ReadToEnd();
                    }
                }
            }

            Console.WriteLine($"Status: {code}");
            Console.WriteLine(responseBody);
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much ! I had certificate error, so I just added a certificate validation handler right before the try/catch block. However, works perfectly now !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.