我已经看到了很多关于这个的问题,并且都指向我使用 ConfigureAwait(false),但即使这样做了,它仍然没有返回任何响应。当我运行调试器时,代码在 PostAsync 处停止并且不会继续我的代码。我在这里做错了吗?这与我通过 HTTPS 调用 API 有关吗?
这是代码:
public async static Task<PaymentModel> AddAsync(Card card)
{
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:", "hidden"))));
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
var cardJson = JsonConvert.SerializeObject(card);
var postRequest = new StringContent(cardJson, Encoding.UTF8, "application/json");
var request = await client.PostAsync(new Uri("https://sample-3rd-party-api/api/endpoint/here"), postRequest).ConfigureAwait(false);
var content = await request.Content.ReadAsStringAsync().ConfigureAwait(false);
}
编辑:
作为对以下评论的回应,代码包含在通过处理程序单击按钮调用的方法 AddAsync(Card card) 中:
public async void OnExecute(object sender, EventArgs args)
{
//some code here
payment = await PaymentModel.AddAsync(card).ConfigureAwait(false);
}
编辑 2:我尝试 ping API,但它返回一个请求超时,但是当我使用 Postman 尝试它时,它做得很好(API 只是一个对所有人开放的沙盒,所以可以分享这个):

编辑 3:
我认为问题在于我没有 SSL 证书来访问 API。我有一个连接到同一个 API 的 PHP 服务器,我必须将 SSL_VERIFYPEER 设置为 false 才能访问它(别担心,我现在添加了一个 cacert,所以它再次为 true)。这里会发生同样的问题吗?如果是这样,我该怎么做才能为我的 xamarin 表单应用程序创建有效证书
