C# 如何使用System.Net.Http发送下面所示的cURL请求?

C# 如何使用System.Net.Http发送下面所示的cURL请求?,c#,http,curl,http-headers,zendesk,C#,Http,Curl,Http Headers,Zendesk,我试图使用Zendesk的票证提交API,在他们的文档中,他们给出了以下cURL示例: curl https://{subdomain}.zendesk.com/api/v2/tickets.json\ -d'{“票证”:{“请求者”:{“姓名”:“客户”,“电子邮件”:thecustomer@domain.com“},”主题“:“我的打印机着火了!”,“评论“:{”正文“:“烟很鲜艳。”}”\ -H“内容类型:application/json”-v-u{email_address}:{pass

我试图使用Zendesk的票证提交API,在他们的文档中,他们给出了以下cURL示例:

curl https://{subdomain}.zendesk.com/api/v2/tickets.json\
-d'{“票证”:{“请求者”:{“姓名”:“客户”,“电子邮件”:thecustomer@domain.com“},”主题“:“我的打印机着火了!”,“评论“:{”正文“:“烟很鲜艳。”}”\
-H“内容类型:application/json”-v-u{email_address}:{password}-X POST

我正在尝试使用System.Net.Http库发出此POST请求:

var httpClient = new HttpClient();
HttpContent httpContent = new StringContent(JsonConvert.SerializeObject(model));
if (httpContent.Headers.Any(r => r.Key == "Content-Type"))
    httpContent.Headers.Remove("Content-Type");
httpContent.Headers.Add("Content-Type", "application/json");
httpContent.Headers.Add("Authorization", String.Format("Basic {0}", Convert.ToBase64String(Encoding.ASCII.GetBytes("{user}:{password}"))));
var httpResult = httpClient.PostAsync(WebConfigAppSettings.ZendeskTicket, httpContent);
当我尝试将授权标头添加到内容时,我不断收到一个错误。我现在明白了HttpContent只应该包含内容类型头


如何创建和发送POST请求,以便使用System.Net.Http库设置内容类型标头、授权标头,并在正文中包含Json?

我使用以下代码构建请求:

HttpContent httpContent = new StringContent(JsonConvert.SerializeObject(new { ticket = model }));
if (httpContent.Headers.Any(r => r.Key == "Content-Type"))
    httpContent.Headers.Remove("Content-Type");
httpContent.Headers.Add("Content-Type", "application/json");
var httpRequest = new HttpRequestMessage()
{
    RequestUri = new Uri(WebConfigAppSettings.ZendeskTicket),
    Method = HttpMethod.Post,
    Content = httpContent
};
httpRequest.Headers.Add("Authorization", String.Format("Basic {0}", Convert.ToBase64String(Encoding.UTF8.GetBytes(@"{username}:{password}"))));
httpResult = httpClient.SendAsync(httpRequest);
基本上,我分别构建内容,添加主体和设置标题。然后,我将身份验证头添加到
httpRequest
对象中。因此,我必须将内容头添加到
httpContent
对象,并将授权头添加到
httpRequest
对象