TrustFailure exception during HTTPWebRequest via HTTPS connection

0.00 avg. rating (0% score) - 0 votes

If an HTTPWebRequest is made via a secure connection (HTTPS) and the certificate is not trusted, a WebException with Exception.Status = TrustFailure will be thrown. To bypass this, we need to explicitly declare a custom certificate policy in a separate class:


Imports System.Net
Imports System.Security.Cryptography.X509Certificates

Public Class TrustAllCertificatePolicy
Implements System.Net.ICertificatePolicy

‘default empty constructor
Public Sub New()
End Sub

Public Function CheckValidationResult(ByVal sp As ServicePoint, ByVal cert As X509Certificate, ByVal req As WebRequest, ByVal problem As Integer) As Boolean Implements ICertificatePolicy.CheckValidationResult
‘return true to accept all certificates. Otherwise examine ‘cert’ and return either TRUE or FALSE according to its properties
Return True
End Function

End Class


Before the call to HTTPWebRequest, explicitly state the custom policy:

System.Net.ServicePointManager.CertificatePolicy = New TrustAllCertificatePolicy()

Reference: http://www.pcreview.co.uk/forums/thread-1226590.php

0.00 avg. rating (0% score) - 0 votes
ToughDev

ToughDev

A tough developer who likes to work on just about anything, from software development to electronics, and share his knowledge with the rest of the world.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>