C# web 请求与 LXD 交互

C# web requests interacting with LXD

我正在尝试通过 C# 与 LXD API 通信。在命令行中,我可以使用

curl -s -k --cert lxd.crt --key lxd.key https://127.0.0.1:40/1.0/endpoint

与API进行交流。我希望完全在 C# 中完成此操作。

发出这样的 Web 请求的最佳方式是什么?或者 use/make cURL 包装器会更好吗?

已解决,可以使用 RestSharp 做到这一点:

    RestClient client;
    public API()
    {
        client = new RestClient("https://127.0.0.1:40/");

        ServicePointManager.Expect100Continue = true;
        ServicePointManager.DefaultConnectionLimit = 9999;
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;
        ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;

        var certFile = "lxd.pfx";
        X509Certificate2 cert = new X509Certificate2(certFile, "(password)");
        client.ClientCertificates = new X509CertificateCollection() { cert };
        client.Proxy = new WebProxy();
    }

    public void Test()
    {
        var restrequest = new RestRequest("1.0/networks", Method.GET);
        IRestResponse response = client.Execute(restrequest);
        Logging.Log(response.Content, "API", LogLevel.INFO);
    }