【问题标题】:Need to disable SSL certificate verification with RestSharp需要使用 RestSharp 禁用 SSL 证书验证
【发布时间】:2017-06-01 16:19:33
【问题描述】:

我已使用 Postman 工具禁用 SSL 证书验证,并发送了包含 4 个参数的发布请求。此方法返回正确的响应,但我似乎无法使用 VB.Net 复制它。我正在使用 RestSharp 连接到 RESTful API,并且正在将认证验证回调设置为使用 ServicePointManager 始终返回 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。
  • @NoAlias 谢谢。我不得不使用:ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 然后它起作用了!
  • 很高兴它成功了。我不想根据猜测将其发布为答案,但由于您确认它有效,我添加了一个比上述评论更彻底的答案。

标签: vb.net certificate restsharp


【解决方案1】:

该错误通常表明ServicePointManager.SecurityProtocol 需要更改。你可以试试 TLS。请注意,此设置是通用的,因此除非您将其更改为 SSL,否则此时一切都将是 TLS。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-04
    • 1970-01-01
    • 2019-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多