How to connect vs2017 android emulator to desktop pc localhost:port#

I have developed a web api in asp.net and webapi. Here is a screen shot showing the port#

I have the emulator running in hyper-v and can browse the internet from the emulator. However when I try to issue an HTTPPost request, I get a long pause then an error message.

Here's the code from my server request:

public static string BaseUrl = "http://192.168.1:63197/api/";
static HttpClient client = new HttpClient();

    public static async Task<string> PostDataAsync(string url, string json)
    {
        try
        {
            var result = await client.PostAsync(BaseUrl + url, new StringContent(
                json,
                Encoding.UTF8,
                "application/json"));
            var x = await result.Content.ReadAsStringAsync();
            System.Diagnostics.Debug.WriteLine(x);
            return x;
        }
        catch (Exception e)
        {
            return null;
        }
    }

Here is the call from the Xamarin Forms PCL in the project:
public LoginViewModel()
{
LoginCommand = new Command(async ()=> await Login());
CreateAccountCommand = new Command(async () => await CreateAccount());
ForgotPasswordCommand = new Command(async () => await ForgotPassword());
}

    async Task Login()
    {
        IsBusyLogin = true;
        var lu = new LoginUserDto();
        lu.EmailAddress = "[email protected]";
        lu.Password = "12345";

        var body = JsonConvert.SerializeObject(lu);

        var result = await ServerRequest.PostDataAsync("User/LoginUser", body);

        IsBusyLogin = false;
     }

What do I need to use for my BaseUrl in this code. I have written nothing on the android project. Here is the list of data adapters on the emulator:

Do I have to enable CORS to get this to work? I am really frustrated. Going at this for days now.

Best Answer

  • JohnBairdJohnBaird US ✭✭
    Accepted Answer

    Well I guess I've struggled long enough with this and some fairy came buy and sprinkled dust on my project. IT IS FINALLY WORKING.....

Answers

  • JohnBairdJohnBaird USMember ✭✭
    Accepted Answer

    Well I guess I've struggled long enough with this and some fairy came buy and sprinkled dust on my project. IT IS FINALLY WORKING.....

Sign In or Register to comment.