I'm beginner and I work one Xamarin app. I try to connect my app one Https server for get a Token, and when I test connection with Postman, all it's okay. But with my app, is a other story..
The web service is : aps.net-web-api-2
And this is my connection code in Xamarin:
using System.Net.Http;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
Button button = FindViewById<Button>(Resource.Id.getButton);
button.Click += async (sender, e) =>
{
try
{
string url = "https://urlexample/foo/bar/Token";
string values = await RequestWeb(url);
}
catch (Exception ex)
{
Log.Info("error with click", ex.ToString());
}
};
}
private async Task<string> RequestHttps(string url)
{
string responseString = string.Empty;
using (HttpClient client = new HttpClient())
{
Dictionary<string,string> values = new Dictionary<string, string>
{
{ "grant_type", "password" },
{ "username", "User1" },
{ "password", "123456" }
};
FormUrlEncodedContent content = new FormUrlEncodedContent(values);
HttpResponseMessage response = await client.PostAsync(url, content);
responseString = await response.Content.ReadAsStringAsync();
}
return responseString;
}
When I test with my code, I recovers my exception with an Try and Catch :
I/error with click(14476): System.Net.WebException: Error: TrustFailure (The authentication or decryption has failed.)
The probleme comes from this line :
HttpResponseMessage response = await client.PostAsync(url, content);
Example with Postman, for this connection, I get a connection Token :
Post -> Headers
- Content_type : application/x-www-form-urlencoded
- grand_type : password
- username : User1
- password : 123456
result :
{ "access_token": "ATT4U2xGQqsdf", "token_type": "bearer", "expires_in": 15 }
I try anothers ways for connection too (example here) And I wondering, when I read a cours for Android (here) it is said that do not create connection whit thread UI, but use another thread. But a lot of examples that I find don't use thread UI, why ? Or I misunderstood one thing ?
I also wonder if the probleme is from my settings ?
Or maybe I'm just totally lost... (haha).
Best regards, Romain