How do you set the Content-Type header for an HttpClient request?

后端 未结 14 2245
暗喜
暗喜 2020-11-22 03:57

I\'m trying to set the Content-Type header of an HttpClient object as required by an API I am calling.

I tried setting the Content-Ty

14条回答
  •  悲哀的现实
    2020-11-22 04:25

    I find it most simple and easy to understand in the following way:

    async Task SendPostRequest()
    {
        HttpClient client = new HttpClient();
        var requestContent = new StringContent();
        requestContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
        var response = await client.PostAsync(, requestContent);
        var responseString = await response.Content.ReadAsStringAsync();
    }
    ...
    
    SendPostRequest().Wait();
    

提交回复
热议问题