C# Xamarin连接Https与POST,asp.net-web-api-2;身份验证或解密失败";
我是初学者,我使用一个Xamarin应用程序。 我尝试连接我的AppOne Https服务器以获取令牌,当我测试与Postman的连接时,一切正常。但在我的应用程序中,是另一回事 web服务是:aps.net-web-api-2 这是我在Xamarin中的连接代码:C# Xamarin连接Https与POST,asp.net-web-api-2;身份验证或解密失败";,c#,android,xamarin,https,asp.net-web-api2,C#,Android,Xamarin,Https,Asp.net Web Api2,我是初学者,我使用一个Xamarin应用程序。 我尝试连接我的AppOne Https服务器以获取令牌,当我测试与Postman的连接时,一切正常。但在我的应用程序中,是另一回事 web服务是:aps.net-web-api-2 这是我在Xamarin中的连接代码: using System.Net.Http; protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle);
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;
}
问题来自这一行:
HttpResponseMessage response = await client.PostAsync(url, content);
例如,对于Postman,对于此连接,我获得一个连接令牌:
Post -> Headers
- Content_type : application/x-www-form-urlencoded
- grand_type : password
- username : User1
- password : 123456
结果:
{ "access_token": "ATT4U2xGQqsdf", "token_type": "bearer", "expires_in": 15 }
我也尝试其他的连接方式()
我想知道,当我读到一本Android课程()时,它说不要创建与线程UI的连接,而是使用另一个线程。
但是我发现很多例子没有使用线程UI,为什么?
还是我误解了一件事
我还想知道问题是否来自我的设置
或者我完全迷路了。。。(哈哈)
致以最良好的祝愿,
Romain请在初始化组件()之前使用这两行代码代码>在你的App.cs代码中
System.Security.Cryptography.AesCryptoServiceProvider AES = new System.Security.Cryptography.AesCryptoServiceProvider();
System.Net.ServicePointManager.ServerCertificateValidationCallback += (o, certificate, chain, errors) => true;
默认情况下,Mono没有受信任的证书,因此您必须跳过错误或添加一个证书来补充您的帖子。请在
InitializeComponent()之前使用这两行代码代码>在你的App.cs代码中
System.Security.Cryptography.AesCryptoServiceProvider AES = new System.Security.Cryptography.AesCryptoServiceProvider();
System.Net.ServicePointManager.ServerCertificateValidationCallback += (o, certificate, chain, errors) => true;
默认情况下,Mono没有可信任的证书,因此您要么跳过错误,要么添加一个证书以补充您的帖子。感谢Zroq的回答!它现在工作得很好,非常感谢您提供的信息!如何“添加补充您帖子的证书”?感谢Zroq的回答!它现在工作得很好,非常感谢您提供的信息!如何“添加补充您的帖子的证书”?