【发布时间】:2020-12-15 05:21:50
【问题描述】:
我正在尝试通过在 c# 中使用以下代码集来调用服务。 我正在使用 .NET Framework 4.6.2。每当我到达代码 (HttpWebResponse)myHttpWebRequest.GetResponse() 时,它都会抛出异常“请求被中止:无法创建 SSL/TLS 安全通道”。我尝试了堆栈溢出中所说的许多解决方案。但没有一个对我没有帮助。从上周开始,我一直在为此苦苦挣扎,然后终于在这里发帖。请任何帮助将不胜感激。 我什至在本地商店添加了证书,但仍然没有用。下面是我的代码。
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
Uri myUri = new Uri("https://CallingUrl");
WebRequest myWebRequest = HttpWebRequest.Create(myUri);
HttpWebRequest myHttpWebRequest = (HttpWebRequest)myWebRequest;
string svcCredentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes("User:Pwd"));
myHttpWebRequest.Headers.Add("Authorization", "Basic " + svcCredentials);
myHttpWebRequest.PreAuthenticate = true;
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
myHttpWebRequest.Method = "POST"
HttpWebResponse httpResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
【问题讨论】:
-
你能让这个在邮递员中工作吗?您是否从作者那里获得了该服务的文档?另外你为什么使用 webrequest 而不是 HttpClient,有特定的用例吗?
-
我已经在邮递员工具中检查了这个,我得到了正确的响应。我也尝试了 HttpClient 然后我也面临同样的问题@MichaelRandall
-
您是否尝试过更改 ServicePointManager.SecurityProtocol
-
是的,我尝试了 SS3、Tls、Tls11、Tls12 但仍然没有用 @MichaelRandall
-
这可能是HttpWebRequest请求的问题,可以尝试添加服务引用来调用服务,而不是直接构造http请求。