需要使用 RestSharp 禁用 SSL 证书验证
Need to disable SSL certificate verification with RestSharp
我已使用 Postman 工具禁用 SSL 证书验证并发送了一个带有 4 个参数的 post 请求。这种方法 returns 是正确的响应,但我似乎无法使用 VB.Net 复制它。我正在使用 RestSharp 连接到 RESTful API,我正在使用 ServicePointManager 将认证验证回调设置为始终 returns true 的函数,但我的问题仍然存在:
Public Function AcceptAllCertifications(sender As Object, certification As X509Certificate,
chain As X509Chain, sslPolicyErrors As SslPolicyErrors) As Boolean
Return True
End Function
Public Function SendRequest() As Boolean
ServicePointManager.ServerCertificateValidationCallback =
New RemoteCertificateValidationCallback(AddressOf AcceptAllCertifications)
Dim client = New RestClient(TokenEndPoint) 'A basic URI endpoint
Dim request = New RestRequest(Method.POST)
request.AddHeader("Content-Type", "application/x-www-form-urlencoded")
request.AddParameter("grant_type", "client_credentials")
request.AddParameter("scope", "write")
request.AddParameter("client_id", "client-app")
request.AddParameter("client_secret", "secret")
response = client.Execute(request)
Console.WriteLine(response.Content)
End Function
我总是得到:
{"The underlying connection was closed: An unexpected error occurred on a send."}
作为回应。我该如何解决这个问题?
该错误通常表示 ServicePointManager.SecurityProtocol 需要更改。你可以试试 TLS。请注意,此设置是通用的,因此此时一切都将是 TLS,除非您将其更改为 SSL。
我已使用 Postman 工具禁用 SSL 证书验证并发送了一个带有 4 个参数的 post 请求。这种方法 returns 是正确的响应,但我似乎无法使用 VB.Net 复制它。我正在使用 RestSharp 连接到 RESTful API,我正在使用 ServicePointManager 将认证验证回调设置为始终 returns true 的函数,但我的问题仍然存在:
Public Function AcceptAllCertifications(sender As Object, certification As X509Certificate,
chain As X509Chain, sslPolicyErrors As SslPolicyErrors) As Boolean
Return True
End Function
Public Function SendRequest() As Boolean
ServicePointManager.ServerCertificateValidationCallback =
New RemoteCertificateValidationCallback(AddressOf AcceptAllCertifications)
Dim client = New RestClient(TokenEndPoint) 'A basic URI endpoint
Dim request = New RestRequest(Method.POST)
request.AddHeader("Content-Type", "application/x-www-form-urlencoded")
request.AddParameter("grant_type", "client_credentials")
request.AddParameter("scope", "write")
request.AddParameter("client_id", "client-app")
request.AddParameter("client_secret", "secret")
response = client.Execute(request)
Console.WriteLine(response.Content)
End Function
我总是得到:
{"The underlying connection was closed: An unexpected error occurred on a send."}
作为回应。我该如何解决这个问题?
该错误通常表示 ServicePointManager.SecurityProtocol 需要更改。你可以试试 TLS。请注意,此设置是通用的,因此此时一切都将是 TLS,除非您将其更改为 SSL。