I created a C# project using
cd /tmp
dotnet new console -o mydotnetapp
cd mydotnetapp
then I replaced the code in Program.cs with code that sends a web request with a custom Content-Type header:
HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "http://localhost:8888/");
request.Headers.Add("Accept", "application/json");
request.Content = new StringContent("18233982904");
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpResponseMessage response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
and I ran it using dotnet run
and I get this error:
/private/tmp/mydotnetapp/Program.cs(8,43): error CS0246: The type or namespace name 'MediaTypeHeaderValue' could not be found (are you missing a using directive or an assembly reference?) [/private/tmp/mydotnetapp/mydotnetapp.csproj]
The build failed. Fix the build errors and run again.
What am I doing wrong? This seems like an issue with me using an old version of the HTTP library, but my .csproj file says I'm using .NET 6
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
CodePudding user response:
it is part of System.Net.Http.Headers' namespace.
you need to add this.
using System.Net.Http.Headers;