-3

localhost is not working, I am using a SQL Server database now, but I don't know what to put in localhost IP, I tried 10.0.2.2 and my ipv4 ip. it doesn't work too.

     HttpClient client = new HttpClient();
     string url = $"https://localhost:xxxxx/api/Feedback?email={feedback.Email}&subject={feedback.Subject}&message={feedback.Message}";

Exception

System.Net.WebException: 'Failed to connect to localhost/127.0.0.1:44330'

Code:

     btnSend.Click += async delegate
            {
                Feedback feedback = new Feedback();
                feedback.Email = edtEmail.Text;
                feedback.Subject = edtSubject.Text;
                feedback.Message = edtMessage.Text;
                HttpClient client = new HttpClient();
                string url = $"https://localhost:xxxxxx/api/Feedback?email={feedback.Email}&subject={feedback.Subject}&message={feedback.Message}";
                
                var uri = new Uri(url);
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                HttpResponseMessage response;
                var json = JsonConvert.SerializeObject(feedback);
                var content = new StringContent(json, Encoding.UTF8, "application/json");
                response = await client.PostAsync(uri, content);
                Clear();

                if (response.StatusCode == System.Net.HttpStatusCode.Accepted)
                {
                    Toast.MakeText(this, "Your Feedback is Saved ", ToastLength.Long).Show();
                }
                else
                {
                    Toast.MakeText(this, "Your Feedback is not Saved", ToastLength.Long).Show();
                }
            };
        }

Image of WebAPI:

Contents of WebAPI

ewan
  • 1
  • 1
  • 1
    Do not use localhost. Use the actual IP or FQDN of the server. This has been discussed repeatedly dozens if not hundreds of times. – Jason May 22 '21 at 20:46
  • I tried my IP but it doesn't work. – ewan May 23 '21 at 11:17
  • Have you read any of the numerous other questions on this topic? Have you verified that your server can accept remote connections? Have you done any basic network debugging to attempt to fix this? – Jason May 23 '21 at 11:41
  • Yes I've tried these basic network debugging. here's my reference but it didn't solve my problem. https://stackoverflow.com/questions/5528850/how-do-you-connect-localhost-in-the-android-emulator – ewan May 23 '21 at 11:49

1 Answers1

0

It Depends on your Dev environment setup, if you are using a real phone then you have to make sure that your PC and your phone are on the same network and the corresponding port number is open in your PC firewall elsewhere the phone can't reach the service.