R
romiko2000
Hi Folks,
I am having a problem, I am not sure how to attach an event to my code,
so that the validate certificate routine runs when the
HttpWebRequest.Create is called.
Please can someone just advise me on how to do this, I would like to
read the target server certificate when I connect to a URL and do some
checks on the expiry date etc. Also if the certificate is ok then it
can connect and carry on withthe code.
Here is the portion of code I need to get working. I would like
MyCertValidationCb to be called as an event when create(url) runs, to
validate the remote certificate.
Private Function SendXML(ByVal countryName As String, ByVal url As
String, ByVal xmlText As String, ByVal cert As X509Certificate, ByVal
NetworkTimeoutMilliseconds As Integer) As Boolean
Try
xmlDoc.LoadXml(xmlText)
'System.Net.ServicePointManager.CertificatePolicy = New
TrustedCertificatePolicy
'ServerCertificateValidationCallback
ServicePointManager.ServerCertificateValidationCallback = New
RemoteCertificateValidationCallback(MyCertValidationCb)
timeNow = Date.Now 'Get the time which will be used by exceptions
wReq = HttpWebRequest.Create(url) ' Connect to URL
wReq.ClientCertificates.Add(cert)
wReq.Timeout = NetworkTimeoutMilliseconds
wReq.Method = "POST" 'Set HTTP Method
wReq.ContentType = "text/xml charset=utf-8" ' Set Content Type
'Open Stream
xmlWriter = New XmlTextWriter(wReq.GetRequestStream(),
System.Text.Encoding.UTF8)
'Send the XML Data
xmlDoc.WriteTo(xmlWriter)
xmlWriter.Close()
'Get Response Stream
timeNow = Date.Now 'If an exception did not occur, grab the time here
wResp = wReq.GetResponse
If TypeOf wResp Is HttpWebResponse Then
httpCode = CType(wResp, HttpWebResponse).StatusCode 'Convert to
HTTPWebResponse
If httpCode = HttpStatusCode.Accepted Or httpCode = HttpStatusCode.OK
Then
interval = Date.Now.Subtract(timeNow).TotalSeconds
secondsToComplete = interval.ToString("f3")
statusMessage = "UP - Response Time: " & secondsToComplete & "
milliseconds"
DataWriter.UpdateAlertTracker(Date.Now, countryName, statusMessage)
Else
statusMessage = "Down"
WindowsEventLogger.LogMessageWarning(countryName & ": " & statusMessage
& vbNewLine & "HTTPCode: " & httpCode)
End If
End If
wResp.Close()
Catch wex As WebException
If Not CType(wex.Response, Net.HttpWebResponse) Is Nothing Then
httpCode = CType(wex.Response, Net.HttpWebResponse).StatusCode
statusMessage = "Down"
WindowsEventLogger.LogMessageWarning(countryName & ": " & statusMessage
& vbNewLine & "HTTPCode: " & httpCode)
Else
httpCode = "n/a"
statusMessage = "Down"
WindowsEventLogger.LogMessageWarning(countryName & ": " & statusMessage
& vbNewLine & "HTTPCode: " & httpCode)
End If
Catch ex As Exception
errorMessage = ex.Message & vbNewLine & ex.StackTrace
statusMessage = "Down"
httpCode = "n/a"
WindowsEventLogger.LogMessageWarning(countryName & ": " & statusMessage
& vbNewLine & "HTTPCode: " & httpCode & "errorMessage: " & vbNewLine &
errorMessage)
Throw
Finally
If Not xmlWriter Is Nothing Then
xmlWriter.Close()
End If
If Not wResp Is Nothing Then
wResp.Close()
End If
End Try
End Function
Public Function MyCertValidationCb(ByVal sender As Object, ByVal
certificate As X509Certificate, ByVal chain As X509Chain, ByVal
sslPolicyErrors As SslPolicyErrors) As Boolean
If ((SslPolicyErrors & SslPolicyErrors.RemoteCertificateChainErrors) =
SslPolicyErrors.RemoteCertificateChainErrors) Then
Return False
ElseIf ((SslPolicyErrors &
SslPolicyErrors.RemoteCertificateNameMismatch) =
SslPolicyErrors.RemoteCertificateNameMismatch) Then
Dim z As Zone
z = Zone.CreateFromUrl(CType(sender,
HttpWebRequest).RequestUri.ToString())
If (z.SecurityZone = System.Security.SecurityZone.Intranet Or
z.SecurityZone = System.Security.SecurityZone.MyComputer) Then
Return True
End If
Return False
End If
Return False
End Function
I am having a problem, I am not sure how to attach an event to my code,
so that the validate certificate routine runs when the
HttpWebRequest.Create is called.
Please can someone just advise me on how to do this, I would like to
read the target server certificate when I connect to a URL and do some
checks on the expiry date etc. Also if the certificate is ok then it
can connect and carry on withthe code.
Here is the portion of code I need to get working. I would like
MyCertValidationCb to be called as an event when create(url) runs, to
validate the remote certificate.
Private Function SendXML(ByVal countryName As String, ByVal url As
String, ByVal xmlText As String, ByVal cert As X509Certificate, ByVal
NetworkTimeoutMilliseconds As Integer) As Boolean
Try
xmlDoc.LoadXml(xmlText)
'System.Net.ServicePointManager.CertificatePolicy = New
TrustedCertificatePolicy
'ServerCertificateValidationCallback
ServicePointManager.ServerCertificateValidationCallback = New
RemoteCertificateValidationCallback(MyCertValidationCb)
timeNow = Date.Now 'Get the time which will be used by exceptions
wReq = HttpWebRequest.Create(url) ' Connect to URL
wReq.ClientCertificates.Add(cert)
wReq.Timeout = NetworkTimeoutMilliseconds
wReq.Method = "POST" 'Set HTTP Method
wReq.ContentType = "text/xml charset=utf-8" ' Set Content Type
'Open Stream
xmlWriter = New XmlTextWriter(wReq.GetRequestStream(),
System.Text.Encoding.UTF8)
'Send the XML Data
xmlDoc.WriteTo(xmlWriter)
xmlWriter.Close()
'Get Response Stream
timeNow = Date.Now 'If an exception did not occur, grab the time here
wResp = wReq.GetResponse
If TypeOf wResp Is HttpWebResponse Then
httpCode = CType(wResp, HttpWebResponse).StatusCode 'Convert to
HTTPWebResponse
If httpCode = HttpStatusCode.Accepted Or httpCode = HttpStatusCode.OK
Then
interval = Date.Now.Subtract(timeNow).TotalSeconds
secondsToComplete = interval.ToString("f3")
statusMessage = "UP - Response Time: " & secondsToComplete & "
milliseconds"
DataWriter.UpdateAlertTracker(Date.Now, countryName, statusMessage)
Else
statusMessage = "Down"
WindowsEventLogger.LogMessageWarning(countryName & ": " & statusMessage
& vbNewLine & "HTTPCode: " & httpCode)
End If
End If
wResp.Close()
Catch wex As WebException
If Not CType(wex.Response, Net.HttpWebResponse) Is Nothing Then
httpCode = CType(wex.Response, Net.HttpWebResponse).StatusCode
statusMessage = "Down"
WindowsEventLogger.LogMessageWarning(countryName & ": " & statusMessage
& vbNewLine & "HTTPCode: " & httpCode)
Else
httpCode = "n/a"
statusMessage = "Down"
WindowsEventLogger.LogMessageWarning(countryName & ": " & statusMessage
& vbNewLine & "HTTPCode: " & httpCode)
End If
Catch ex As Exception
errorMessage = ex.Message & vbNewLine & ex.StackTrace
statusMessage = "Down"
httpCode = "n/a"
WindowsEventLogger.LogMessageWarning(countryName & ": " & statusMessage
& vbNewLine & "HTTPCode: " & httpCode & "errorMessage: " & vbNewLine &
errorMessage)
Throw
Finally
If Not xmlWriter Is Nothing Then
xmlWriter.Close()
End If
If Not wResp Is Nothing Then
wResp.Close()
End If
End Try
End Function
Public Function MyCertValidationCb(ByVal sender As Object, ByVal
certificate As X509Certificate, ByVal chain As X509Chain, ByVal
sslPolicyErrors As SslPolicyErrors) As Boolean
If ((SslPolicyErrors & SslPolicyErrors.RemoteCertificateChainErrors) =
SslPolicyErrors.RemoteCertificateChainErrors) Then
Return False
ElseIf ((SslPolicyErrors &
SslPolicyErrors.RemoteCertificateNameMismatch) =
SslPolicyErrors.RemoteCertificateNameMismatch) Then
Dim z As Zone
z = Zone.CreateFromUrl(CType(sender,
HttpWebRequest).RequestUri.ToString())
If (z.SecurityZone = System.Security.SecurityZone.Intranet Or
z.SecurityZone = System.Security.SecurityZone.MyComputer) Then
Return True
End If
Return False
End If
Return False
End Function