Ivaylo Kisyov
Ivaylo Kisyov

Reputation: 21

How to make cURL call in C#

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.

Upvotes: 0

Views: 13573

Answers (1)

Martin Brown
Martin Brown

Reputation: 25310

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);
        }
    }
}

Upvotes: 2

Related Questions