0
On my first screen of mine app mobile
what I’m doing is the screen of Login, but whenever I try to log the return is
Statuscode: 404,
Reasonphrase: 'Not Found'
I have confirmed and the uri
is correct, access via Postman normally, I began to suspect that it is something lacking to configure in the project or manifest of app
, 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, 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 code above:
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;
}
}
}
}
If I understand correctly, this console is working, right? If so, we need the code that isn’t working to find out why. If possible, you could [Dit] your question and add this information?
– Randrade
Try to put the content like this, Jsonconvert.Serializeobject(pairs).
– Marco Vinicius Soares Dalalba
@Randrade, I put the code you asked for.
– rhgm
You specified the manifest permission on android to authorize internet access ? android.permission.INTERNET
– Junior Porfirio
Try the following on your project
Android - MainActivity.cs
add in the beginning methodOnCreate
the codeServicePointManager.ServerCertificateValidationCallback +=
 (sender, cert, chain, sslPolicyErrors) => true;
If it works add as a response.– rubStackOverflow
@Juniorporpirio, I downloaded the manifest and did not find this option, but as I understood on this link here is a permission that is already included: https://developer.xamarin.com/recipes/android/general/projects/add_permissions_to_android_manifest/
– rhgm
@Marcoviniciussoaresdalalba, If I do this the content will be a string type, Postasync does not receive it, receives an Httpcontent, will not work...
– rhgm
Yes is included to authorize in android manifest. For this you must in VS right click on the Droid project and go to properties -> Android Manifest and enable INTERNET check.
– Junior Porfirio
@rubStackOverflow, put the code and gave anyway, nothing changed.
– rhgm
@rhgm you managed to perform the configuration on Android Manifest and test the application ?
– Junior Porfirio
@Juniorporpirio, already downloaded the Android Manifest and did not find this option, but I edited the Androidmanifest.xml and added:
<uses-permission android:name="android.permission.INTERNET" />
Still nothing done, I’m having the same problem.– rhgm
@rhgm, tries to put the string result into a variable with await. ex: var stringresponse = await Response.Content.Readasstringasync(); or remove async to test and call directly in the result as it did on the console.
– Junior Porfirio
@Juniorphyrus is the same error message you already informed
"{\"statusCode\":404,\"error\":\"Not Found\"}"
– rhgm