2017-06-01 666 views
0

我已经使用Postman工具禁用了SSL证书验证,并发送了带有4个参数的发布请求。此方法返回正确的响应,但我似乎无法复制这使用VB.Net。我使用RestSharp连接到一个RESTful API和我设置的认证验证回调到一个函数的,使用ServicePointManager总是返回true,但我的问题仍然存在:需要使用RestSharp禁用SSL证书验证

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."} 

作为回应。我怎样才能解决这个问题?

+1

尝试搞乱ServicePointManager.SecurityProtocol的值。它可能需要TLS。 – N0Alias

+0

@NoAlias谢谢。我不得不使用: 'ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12' 然后它工作! – Mayron

+1

很高兴工作。我不想将它作为基于猜测的答案发布,但是由于您确认了它的工作,我添加了比上述评论更彻底的答案。 – N0Alias

回答