0

Recently I have been trying to debug a bit of a legacy app that was handed over for support to me.

I am facing a peculiar issue at the moment, -correct me if I am wrong- it seems like the Xamarin app is being refused by a separate API when it's trying to make a call to it, so here is how things look like:

Got this function that's supposed to call another one in the api:

public static string Post(string uriString, string content = "", string accessToken = "", string contentType = "application/json; charset=utf-8")
        {
            var httpWebRequest = (HttpWebRequest)WebRequest.Create(uriString);
            httpWebRequest.Method = WebRequestMethods.Http.Post;
            httpWebRequest.ContentType = contentType;
            httpWebRequest.Accept = "application/json";
            httpWebRequest.Timeout = Timeout;

            if (!string.IsNullOrEmpty(accessToken))
            {
                httpWebRequest.PreAuthenticate = true;
                httpWebRequest.Headers.Add("Authorization", "Bearer " + accessToken);
            }

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                streamWriter.Write(content);
                streamWriter.Flush();
            }

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader =
                new StreamReader(httpResponse.GetResponseStream() ?? throw new InvalidOperationException()))
            {
                return streamReader.ReadToEnd();
            }
        }

The main issue is with this chunk:

 using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                streamWriter.Write(content);
                streamWriter.Flush();
            }

Throws me the following error:

System.Net.WebException: 'Error: ConnectFailure (Connection refused)'

The connection the app is using looks like this:

public static string BaseApiAddress = "https://127.0.0.1:44309/";

The API is running on:

"ApplicationUrl": "localhost:44309",

To explain the title, I have used the published web API and the app is working correctly with that one. This makes me thinks there is a weird communication blocker on localhost.

Beyond that, I have used Postman to send a request to localhost:44309 and that was picked up by the debugger. So individually they work just fine, but for some reason things fall apart when debugging both on localhost.

Has anyone come across this before? Am I missing something obvious?

Thank you in advance!

BenjaminD
  • 29
  • 1
  • 7
  • Can you replace `localhost:44309` with `https://127.0.0.1:44309` or `https://localhost:44309`? We've had a similar issue recently in a project and it was simply due to the missing *https://* prefix – Julian Oct 17 '22 at 11:42

1 Answers1

2

127.0.0.1 or localhost on the Emulator/Device means localhost on that specific device, not on your local machine.

To reach localhost for your machine you need to use 10.0.2.2 instead.

Cheesebaron
  • 24,131
  • 15
  • 66
  • 118