自定义标题为Httpclient请求

如何将自定义标头添加到HttpClient请求。 我正在使用PostAsJsonAsync方法发布json。 我需要添加的自定义标题是

"X-Version: 1" 

这是我迄今为止所做的:

 using (var client = new HttpClient()) { client.BaseAddress = new Uri("https://api.clickatell.com/"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "xxxxxxxxxxxxxxxxxxxx"); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var response = client.PostAsJsonAsync("rest/message", svm).Result; } 

我find了我的问题的答案。

 client.DefaultRequestHeaders.Add("X-Version","1"); 

这应该添加一个自定义标题到您的请求

  var request = new HttpRequestMessage { RequestUri = new Uri(EnvironmentSetup.BaseAddress + "Folder"), Method = HttpMethod.Post, Headers = { { HttpRequestHeader.Authorization.ToString(), "Basic " + EnvironmentSetup.GetAuthToken()}, { HttpRequestHeader.ContentType.ToString(), "multipart/mixed" } }, Content = new MultipartContent { new ObjectContent<FolderWithDocuments>(new FolderWithDocuments {Status = FolderStatus.Draft, Users = new List<User> {EnvironmentSetup.User1, EnvironmentSetup.User2}}, new JsonMediaTypeFormatter(), "application/json"), new ByteArrayContent(ResourceHelper.ReadResourceToByteArray("blank.pdf")) {Headers = {{"Content-Type", "application/Executable"}, {"Content-Disposition", "form-data; filename=\"test.pdf\""}}} } };