我正在嘗試使用 RestSharp 調(diào)用 REST Web 服務(wù)。以下是我的代碼: ServicePointManager.Expect100Continue = true; ServicePointManager.DefaultConnectionLimit = 9999; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3; ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true; var client = new RestClient(url); var certificate = new X509Certificate(); certificate.Import(Properties.Resources.cert, "password", X509KeyStorageFlags.PersistKeySet); client.ClientCertificates = new X509CertificateCollection() { certificate }; var request = new RestRequest(Method.POST); request.AddHeader("Connection", "keep-alive"); request.AddHeader("Content-Type", "text/plain"); request.AddParameter("undefined", $"param={param}", ParameterType.RequestBody); IRestResponse response = await client.ExecutePostTaskAsync(request); return response;但是,我收到錯(cuò)誤 - 請(qǐng)求已中止:無(wú)法創(chuàng)建 SSL/TLS 安全通道。我也嘗試搜索此錯(cuò)誤,但在所有帖子中,人們建議使用 -SecurityProtocolType.Tls12和ServicePointManager.Expect100Continue = true。我已經(jīng)在我的代碼中使用了這些,但我仍然看到錯(cuò)誤。另外,如果我嘗試使用 Postman 發(fā)出類(lèi)似的請(qǐng)求,它工作得很好。我還缺少什么嗎?
1 回答

滄海一幻覺(jué)
TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超5個(gè)贊
找到了。C# 要求將證書(shū)導(dǎo)入到用戶(hù)存儲(chǔ)中。我錯(cuò)過(guò)了這部分。將 pfx 導(dǎo)入用戶(hù)存儲(chǔ)后,對(duì)于第一個(gè)請(qǐng)求,操作系統(tǒng)提示我允許該請(qǐng)求,后續(xù)請(qǐng)求完美運(yùn)行。
- 1 回答
- 0 關(guān)注
- 795 瀏覽
添加回答
舉報(bào)
0/150
提交
取消