C
C.W.
I am trying to connect to a remote server in order to perform an http post
inside my asp.net page. However, I consistently get the following error:
[SocketException (0x2746): An existing connection was forcibly closed by the
remote host]
System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32
size, SocketFlags socketFlags)
System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32
size)
[IOException: Unable to read data from the transport connection.]
System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32
size)
System.Net.TlsStream.ForceRead(Byte[] space, Int32 offset, Int32 amount)
System.Net.TlsStream.ReadFullRecord(Byte[] buffer, Int32 length)
System.Net.TlsStream.Handshake(ProtocolToken message)
[WebException: The underlying connection was closed: Could not establish
secure channel for SSL/TLS.]
System.Net.HttpWebRequest.CheckFinalStatus()
System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult)
System.Net.HttpWebRequest.GetRequestStream()
......
The offending line is Dim reqWriter As StreamWriter = New
StreamWriter(req.GetRequestStream())
The code snippet is as follows:
Dim req As HttpWebRequest
Dim res As HttpWebResponse
Dim XMLStr="...." --some XML doc
req = WebRequest.Create(https://someurl)
req.Method = "POST"
req.ContentType = "text/xml"
'Dim X509Cert As X509Certificate =
X509Certificate.CreateFromCertFile(ConfigurationSettings.AppSettings("CertfilePath"))
Dim X509Cert As X509Certificate =
X509Certificate.CreateFromCertFile("c:\\mycert.der")
req.ClientCertificates.Add(X509Cert)
ServicePointManager.CertificatePolicy = New AcceptAllCertificatesPolicy
Dim reqWriter As StreamWriter = New StreamWriter(req.GetRequestStream())
reqWriter.Write(XMLStr)
reqWriter.Close()
res = req.GetResponse
Dim ResponseXML As String
If res.StatusCode = HttpStatusCode.OK Then
Dim resReader As StreamReader = New StreamReader(res.GetResponseStream)
ResponseXML = resReader.ReadToEnd()
resReader.Close()
Me.TextBox1.Text = ResponseXML
Else
Throw New Exception("Status code =" & res.StatusCode & "XML Request=" &
XMLStr & "XML Response=" & ResponseXML)
End If
Class AcceptAllCertificatesPolicy
Implements ICertificatePolicy
Public Function CheckValidationResult(ByVal srvPoint As
System.Net.ServicePoint, ByVal certificate As
System.Security.Cryptography.X509Certificates.X509Certificate, ByVal request
As System.Net.WebRequest, ByVal certificateProblem As Integer) As Boolean
Implements System.Net.ICertificatePolicy.CheckValidationResult
Return True
End Function
End Class
The same piece of code (identical unmodified) pasted into a win form runs
perfectly without any hitch.
Does anyone have any idea how this could be the case?
Thanks in advance
inside my asp.net page. However, I consistently get the following error:
[SocketException (0x2746): An existing connection was forcibly closed by the
remote host]
System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32
size, SocketFlags socketFlags)
System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32
size)
[IOException: Unable to read data from the transport connection.]
System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32
size)
System.Net.TlsStream.ForceRead(Byte[] space, Int32 offset, Int32 amount)
System.Net.TlsStream.ReadFullRecord(Byte[] buffer, Int32 length)
System.Net.TlsStream.Handshake(ProtocolToken message)
[WebException: The underlying connection was closed: Could not establish
secure channel for SSL/TLS.]
System.Net.HttpWebRequest.CheckFinalStatus()
System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult)
System.Net.HttpWebRequest.GetRequestStream()
......
The offending line is Dim reqWriter As StreamWriter = New
StreamWriter(req.GetRequestStream())
The code snippet is as follows:
Dim req As HttpWebRequest
Dim res As HttpWebResponse
Dim XMLStr="...." --some XML doc
req = WebRequest.Create(https://someurl)
req.Method = "POST"
req.ContentType = "text/xml"
'Dim X509Cert As X509Certificate =
X509Certificate.CreateFromCertFile(ConfigurationSettings.AppSettings("CertfilePath"))
Dim X509Cert As X509Certificate =
X509Certificate.CreateFromCertFile("c:\\mycert.der")
req.ClientCertificates.Add(X509Cert)
ServicePointManager.CertificatePolicy = New AcceptAllCertificatesPolicy
Dim reqWriter As StreamWriter = New StreamWriter(req.GetRequestStream())
reqWriter.Write(XMLStr)
reqWriter.Close()
res = req.GetResponse
Dim ResponseXML As String
If res.StatusCode = HttpStatusCode.OK Then
Dim resReader As StreamReader = New StreamReader(res.GetResponseStream)
ResponseXML = resReader.ReadToEnd()
resReader.Close()
Me.TextBox1.Text = ResponseXML
Else
Throw New Exception("Status code =" & res.StatusCode & "XML Request=" &
XMLStr & "XML Response=" & ResponseXML)
End If
Class AcceptAllCertificatesPolicy
Implements ICertificatePolicy
Public Function CheckValidationResult(ByVal srvPoint As
System.Net.ServicePoint, ByVal certificate As
System.Security.Cryptography.X509Certificates.X509Certificate, ByVal request
As System.Net.WebRequest, ByVal certificateProblem As Integer) As Boolean
Implements System.Net.ICertificatePolicy.CheckValidationResult
Return True
End Function
End Class
The same piece of code (identical unmodified) pasted into a win form runs
perfectly without any hitch.
Does anyone have any idea how this could be the case?
Thanks in advance