403 may also be caused by TLS issues. To verify, you should check the text of the WebException.Response object.
catch (WebException ex)
{
if (ex.Response != null)
{
var response = ex.Response;
var dataStream = response.GetResponseStream();
var reader = new StreamReader(dataStream);
var details = reader.ReadToEnd();
}
}
If it is TLS then try adding this to your code to force TLS1.2.
For .net4:
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
For .net4.5 or later:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
本文指导如何通过检查WebException的响应对象,诊断并修复因TLS问题导致的403错误。提供针对.NET 4和4.5+版本的代码示例,强制配置TLS 1.2以确保安全连接。
1855

被折叠的 条评论
为什么被折叠?



