By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,313 Members | 1,092 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,313 IT Pros & Developers. It's quick & easy.

exception using HTTPWebRequest with SSL

P: n/a

I'm using the HTTPWebRequest class to connect to a web site with SSL. I
first manually connected to the site and installed the certificate on my
computer, and then use the X509Certificate class to add the certificate to
the HTTPWebRequest, but I continue to get the exception:

"The underlying connection was closed: Could not establish trust
relationship for the SSL/TLS secure channel."

Inner Exception:
"The remote certificate is invalid according to the validation

KB895971 says the certificate should be installed in the LOCAL_MACHINE
registry hive, but it got installed in the CURRENT_USER registry hive.

Is this the problem?

How do I install the certificate in the LOCAL_MACHINE hive instead of the

I have tested the code below on a non-SSL site and it works fine.
Sub SendRequest()

Dim sHTTPResponse As String = ""

Dim url As String = ""

Dim myHttpWebRequest As HttpWebRequest

Dim myHttpWebResponse As HttpWebResponse

myHttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)

myHttpWebRequest.Method = "POST"

Dim postData As String = "username" + ChrW(61) + "uname"

postData += "&password" + ChrW(61) + "pwd"

Dim encoding As New ASCIIEncoding()

Dim byte1 As Byte() = encoding.GetBytes(postData)

' Set the content type of the data being posted.

myHttpWebRequest.ContentType = "application/x-www-form-urlencoded"

' Set the content length of the string being posted.

myHttpWebRequest.ContentLength = byte1.Length

Dim cert As X509Certificate =
X509Certificate.CreateFromCertFile("C:\Documents and Settings\xxx\My


Dim newStream As Stream = myHttpWebRequest.GetRequestStream()

newStream.Write(byte1, 0, byte1.Length)

' Sends the request and waits for a response.

myHttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)

Dim sr As New StreamReader(myHttpWebResponse.GetResponseStream)

Dim res As String = sr.ReadToEnd

Me.TextBox1.Text = res

End Sub
Feb 22 '07 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.