473,385 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

The underlying connection was closed: Could not establish secure channel for SSL/TLS

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(IAsy ncResult 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(ConfigurationSe ttings.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.X509 Certificate, ByVal request
As System.Net.WebRequest, ByVal certificateProblem As Integer) As Boolean
Implements System.Net.ICertificatePolicy.CheckValidationResul t

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

Nov 19 '05 #1
1 6042
I have figured out the reason for the error, and have put in place a
workaround.

The reason that the code is failing in asp.net but not in win form is
because asp.net uses aspnet account and my private key certificate is only
installed for my account. Hence by modifying the processModel element of
machine.config file, I was able to run the code successfully. However, that
is not a long term solution. Can anyone point me to some resources on how to
install certificate for aspnet account (or any other account for that
matter).

Thanks in advance

"C.W." <c.w@nospam_com> wrote in message
news:OZ**************@TK2MSFTNGP10.phx.gbl...
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(IAsy ncResult 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(ConfigurationSe ttings.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.X509 Certificate, ByVal
request
As System.Net.WebRequest, ByVal certificateProblem As Integer) As Boolean
Implements System.Net.ICertificatePolicy.CheckValidationResul t

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

Nov 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Bill | last post by:
I'm running a C#.Net application that is using the HttpWebRequest to upload an xml file to a https site with FIPS complicancy turned on. On the "GetRequestStream()" method I get: "The underlying...
4
by: konsu | last post by:
hello, my client code communicates with a web service through a https connection. the code has a stub derived from SoapHttpClientProtocol. It also sets a certificate policy handler that allows...
1
by: Mark Richards | last post by:
Hi, I am interfacing with another company using web services (they are a java shop). We've been doing this for over 2 years, but a couple months age we started getting this error randomly. It...
1
by: Rob Thompson | last post by:
Hi, We are using web services that are secured using SSL, everything works fine, but every now and then we get an error: >>START<< System.Net.WebException: The underlying connection was...
0
by: Ryan Michela | last post by:
Hello all, I am trying to access our SOAP web service over an HTTPS connection and am getting the infamous "Could not establish secure channel for SSL/TLS" error. The client program works at all...
0
by: Chris | last post by:
I am seeing the error "Could not establish secure channel for SSL/TLS" intermittently in my application. I am simply trying to connect to a HTTP server via SSL. Five out of ten times it throws...
0
by: Nathan Alden | last post by:
This error will appear intermittently and seemingly for no reason when my Web service tries to contact another Web service over the Internet using SSL. I'd say about 80-90% of all the requests work...
7
by: Jim Butler | last post by:
I have this error that is happening on all of our web servers (production included). It basically started occurring once we loaded 2005 sql client tools, asp.net 2.0 (and all related prerequistes)...
4
by: Sin Jeong-hun | last post by:
I don't get the message so it's hard to debug that, but some of my clients report that they get "The underlying connection was closed unexpectedly" exception. According to this site (http://...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.