2017-06-01 659 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

回答