Post API gives error on sending the request

ahmed omar 181 Reputation points
2023-03-31T17:19:41.24+00:00

Hello,

I tried to post to API but the C# code give me error "Failed to connect to localhost/127.0.0.1:7091"

while if I tried to use Postman , it will work,

for the recieved Json in APIService

{\"FullName\":\"Mark\",\"About\":null,\"Email\":\"aaa@kkk.com\",\"Image\":null,\"Phone\":null,\"Password\":\"1111\",\"Addresss\":null,\"Birthdat\":null,\"Role\":null,\"Id\":null}"

for the APIService

public class ApiServices
    {
        public static async Task<bool> RegisterUser(string FullName, string Email, string Password,string Phone) 
        {
            try
            {
                var register = new User()
                {
                    Email = Email,
                    Password = Password,
                    FullName = FullName,
                    Phone = Phone,
                    Image = "",
                    About = "",
                    Addresss = "",
                    Birthdat = "",
                    Role = "2",
                    CreatedDate = DateTime.Now.ToString("yyyy-MM-dd")
                };
                var httpClient = new HttpClient();
                var json = JsonConvert.SerializeObject(register);
                var content = new StringContent(json,Encoding.UTF8, "application/json");
                
               
                var response = await httpClient.PostAsync("
                   https://localhost:7091/api/Woners/AddWoners", content);
                if (!response.IsSuccessStatusCode) return false;
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message); // give error Failed to connect to localhost/127.0.0.1:7091"
                return false;
            }
        }

for the view model

private async void SignUpAsync()
        {

            var user = new User()
            {
                FullName=FullName, Phone=Phone, Email=Email,Password=Password
            };
         
             var responce=await ApiServices.RegisterUser(FullName, Email, Password,Phone);
              
}
                {
                    Console.WriteLine("fals");
                }
            
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,594 questions
C#
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.
11,010 questions
ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
343 questions
{count} votes

1 answer

Sort by: Most helpful
  1. ahmed omar 181 Reputation points
    2023-03-31T18:24:25.91+00:00

Your answer

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