Problems accessing an api web in a Xamarin Forms project

0

In my first screen of my app mobile I'm doing is the Login screen, however whenever I try to log the return it is

  

StatusCode: 404,

     

ReasonPhrase: 'Not Found'

I have already confirmed and uri is correct, access via postman normally, I started to suspect that something is missing to configure in the project or app manifest, I already tested the project Android and UWP all give the same problem.

What gave me this certainty is that I created a console app just to test access to api , everything works normally, it follows the code:

using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Net.Http;

namespace app1
{
    class Program
    {
        static void Main(string[] args)
        {
            var url = "https://api.dominio.com.br/login";
            var userName = "[email protected]";
            var password = "12345678";

            var token = GetToken(url, userName, password);
        }

        static string GetToken(string url, string userName, string password)
        {
            var pairs = new List<KeyValuePair<string, string>>
                    {
                        new KeyValuePair<string, string>( "username", userName ),
                        new KeyValuePair<string, string> ( "password", password )
                    };
            var content = new FormUrlEncodedContent(pairs);

            using (var client = new HttpClient())
            {
                var response = client.PostAsync(url, content).Result;
                JObject o = JObject.Parse(response.Content.ReadAsStringAsync().Result);
                return (string)o["token"];
            }
        }
    }
}

How can I solve this problem?

On request I will put the code that is not working, although it is the same as the above code:

using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;

namespace AppMobile.Services
{
    public class Auth : IAuthenticate
    {
        const string Url = "https://api.dominio.com.br/Login";

        public async Task<string> GetToken(string user, string pass)
        {
            var pairs = new List<KeyValuePair<string, string>>
            {
                new KeyValuePair<string, string>( "username", user ),
                new KeyValuePair<string, string> ( "password", pass )
            };
            var content = new FormUrlEncodedContent(pairs);
            using (var client = new HttpClient())
            {
                var response = await client.PostAsync(Url, content);
                JObject obj = JObject.Parse(response.Content.ReadAsStringAsync().Result);
                var token = (string)obj["token"];

                return token;
            }
        }
    }
}
    
asked by anonymous 13.01.2017 / 12:19

1 answer

0

I could solve by changing the post so as not to rotate asynchronously:

var response = await client.PostAsync(Url, content).ConfigureAwait(false);

So it worked in the console application, there it was synchronous. I have not found anything in the documentation that explains better why this should work asynchronously ...

    
16.01.2017 / 18:43