如何将此curl命令转换为C#?

时间:2018-10-20 14:53:49

标签: c# azure curl asp.net-web-api azure-devops-rest-api

curl -x http://ip:port -key mykey.key -cert mycert.cert https://url -h“ exampleheader:examplevalue”

1 个答案:

答案 0 :(得分:0)

您可以尝试这样的事情

更新代码

// Use this if you are in a web context
// var certificatePath = System.Web.HttpContext.Current.Server.MapPath(
//                  "your_client_certificate_path"),

// Use this in a non web context. 
// certificatePath should be the same value as the parameter you are using 
// in your command line
//  -cert mycert.cert   <------ This one
var certificatePath = "your_physical_path_file_to_mycert.cert";

// Certificate from file 
var _clientCertificate = new X509Certificate2(
                          certificatePath,
                          "your_client_certificate_key");

// Web handler
var handler = new System.Net.Http.WebRequestHandler();
handler.ClientCertificates.Add(_clientCertificate);

// Http Client
var _httpClient = new System.Net.Http.HttpClient(handler);
_httpClient.DefaultRequestHeaders.Add("Header Key", "Header Value");

// Requests
_httpClient.GetAsync("your_request_URI");