How to use System.Net.Http?

Alexandre Dantas 40 Reputation points
2023-12-15T05:32:49.7933333+00:00

that was my first code:

//using System.Threading.Tasks;
//using System.Net.Http;
//using System;

namespace Program
{
    class EmailValidation
    {
        static async Task Main(string[] args)
        {
            string request = "https://api.eva.pingutil.com/email?email=";
            try
            {
                HttpClient http = new HttpClient();
                Console.Write("Type your email: ");
                string email = Console.ReadLine();
                request += email;
                HttpResponseMessage response = await http.GetAsync(request);
                if (response.IsSuccessStatusCode)
                {
                    Console.WriteLine("Response is okay!");
                }
                else Console.WriteLine("Response isn't okay!");
                Console.WriteLine(request);
                string output = await response.Content.ReadAsStringAsync();
                Console.WriteLine(output);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    };
}


But, i get this error:

Program.cs(2,18): error CS0234: The type or namespace name 'Task' could not be found (are you missing a using directive or an assembly reference?)


If i put the using directives, i get the same error but for "http doesn't exist in namespace System.Net".

How do i fix this?

Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

Answer accepted by question author
  1. Bruce (SqlWork.com) 81,971 Reputation points Volunteer Moderator
    2023-12-15T23:40:20.88+00:00

    what version of the .net framework? your code worked without any packages in a .net 8 console app.

    try:

    mkdir test
    cd test
    dotnet new console

    edit program.cs then

    dotnet run

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.