question

VuyiswaMaseko-8882 avatar image
0 Votes"
VuyiswaMaseko-8882 asked WenyanZhang-MSFT edited

Misused header name. Make sure request headers are used with HttpRequestMessage, response

Good Day all

i have the following code

 // HttpClient is intended to be instantiated once per application, rather than per-use. See Remarks.
 static readonly HttpClient httpClient = new HttpClient();

 public async Task<List<USERS>> Get_All_Customers( )
             {
                    
                 List<USERS> results = null;
                 try
                 {
                     USERS m_model = new USERS
                     {
                         USER_ID = 1,
                         CURRENT_DATETIME = '2021/07/01'
                     };
                     var httpClient = new HttpClient();
                    
                     httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                                     
                                     
                     var Json = JsonConvert.SerializeObject(m_model, new JsonSerializerSettings
                     {
                         ContractResolver = new DefaultContractResolver
                         {
                             IgnoreSerializableAttribute = false
                         }
                     });
                    
                    
                     HttpContent httpContent = new StringContent(Json, Encoding.UTF8, "application/json");
                     httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=utf-8");
                    
  var response =  await httpClient.PostAsync(string.Format("https://www.myserver.com/api/MainCustomers_OnlineDates/Get_All_Customers"), httpContent);
        
                     if (response.StatusCode == System.Net.HttpStatusCode.OK)
                     {
                         var content = await response.Content.ReadAsStringAsync();
                         results = JsonConvert.DeserializeObject<List<USERS>>(content);
                     }
                 }
                 catch (Exception ex)
                 {
                     await Task.Run(() => Crashes.TrackError(ex));
                 }
                    
                 return results;
             }


and this code reports the following error in AppCentre

System.InvalidOperationException: Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.


OS :Android 7.1.2

Thanks

dotnet-xamarinformsdotnet-androiddotnet-aspnet-webapi
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

This error means that you should set Content-Type header, try to add the following code to your Class. Since this address "https://www.myserver.com/api/MainCustomers_OnlineDates/Get_All_Customers" can’t be reached, would you mind sharing a basic reproducible demo to me so that I can test ?

 httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
 client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

refer to
https://forums.asp.net/t/1772062.aspx?DelegatingHandler+Request+Content+Type+throws+Misused+header+name+Make+sure+request+headers+are+used+with+HttpRequestMessage+

0 Votes 0 ·

0 Answers