|
Sometimes you will need to process secure web request to a site with certificate, which does not exactly match default security policy. The server certificate can be expired site host does not match cn, the server certificate can be issued by untrusted authority. If you access the ssl URL by Internet Explorer or other browser, you will usually ge the message with security warning.
If you want to process such URL with VB Net, you have to specify own ICertificatePolicy:
Imports System.Net
Imports System.Web
Imports System.Security.Cryptography.X509Certificates
Imports System.Runtime.InteropServices
Public Class MyAcceptCertificatePolicy
Implements ICertificatePolicy
Private Enum eCertificateProblem As Integer
CertNone = 0
CertEXPIRED = &H800B0101
CertVALIDITYPERIODNESTING = &H800B0102
CertROLE = &H800B0103
CertPATHLENCONST = &H800B0104
CertCRITICAL = &H800B0105
CertPURPOSE = &H800B0106
CertISSUERCHAINING = &H800B0107
CertMALFORMED = &H800B0108
CertUNTRUSTEDROOT = &H800B0109
CertCHAINING = &H800B010A
CertREVOKED = &H800B010C
CertUNTRUSTEDTESTROOT = &H800B010D
CertREVOCATION_FAILURE = &H800B010E
CertCN_NO_MATCH = &H800B010F
CertWRONG_USAGE = &H800B0110
CertUNTRUSTEDCA = &H800B0112
End Enum
Public Overridable Function CheckValidationResult( _
ByVal srvPoint As ServicePoint, _
ByVal certificate As X509Certificate, _
ByVal request As WebRequest, _
ByVal certificateProblem As Integer) As Boolean _
Implements ICertificatePolicy.CheckValidationResult
Dim cp As eCertificateProblem = certificateProblem
'this accepts certificates with untrusted root,
' And If cn does Not match host name
Select Case cp
Case eCertificateProblem.CertUNTRUSTEDROOT, _
eCertificateProblem.CertCN_NO_MATCH
Return True
Case eCertificateProblem.CertNone
Return True
End Select
Return False
End Function
End Class
Public Class Conector
Public Shared Function GetEncodingFromCT(ByRef ContentType As String) As String
On Error Resume Next
Return Split(ContentType, "charset=", 2, CompareMethod.Text)(1).Trim("""")
End Function
Public Function GetWebData(ByVal URL As String) As String
ServicePointManager.CertificatePolicy = New MyAcceptCertificatePolicy
'create a web request To the URL
Dim Req As HttpWebRequest = HttpWebRequest.Create(URL)
'get a response from web site
Dim Response As HttpWebResponse = Req.GetResponse()
'check encoding of the response.
Dim sEncoding As String = response.ContentEncoding
If Len(sEncoding) = 0 Then sEncoding = GetEncodingFromCT(Response.ContentType)
'read the response stream To end using the response stream
Dim Reader As New StreamReader(Response.GetResponseStream(), _
System.Text.Encoding.GetEncoding(sEncoding))
Dim sResponse As String = Reader.ReadToEnd()
'close handles
Reader.Close()
Response.Close()
End Function
End Class
|
See also for 'Process untrusted, expired or not-match certificates with HttpWebRequest and ASP.Net' article Copyright and use this code
The source code on this page and other samples at https://www.motobit.com/tips/
are a free code, you can use it as you want: copy it, modify it, use it in your products, ...
If you use this code, please:
1. Leave the author note in the source.
or
2. Link this sample from you page.
|