exception using HTTPWebRequest with SSL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • BillE

    #1

    exception using HTTPWebRequest with SSL

    VS2005 VB.net

    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
    procedure."

    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
    CURRENT_USER hive?

    Thanks
    Bill
    ----------------------------------------------------------------------------------------------
    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 = "https://www.abc.com/login.asp"

    Dim myHttpWebReques t As HttpWebRequest

    Dim myHttpWebRespon se As HttpWebResponse

    myHttpWebReques t = CType(WebReques t.Create(url), HttpWebRequest)

    myHttpWebReques t.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.GetByt es(postData)

    ' Set the content type of the data being posted.

    myHttpWebReques t.ContentType = "applicatio n/x-www-form-urlencoded"

    ' Set the content length of the string being posted.

    myHttpWebReques t.ContentLength = byte1.Length

    Dim cert As X509Certificate =
    X509Certificate .CreateFromCert File("C:\Docume nts and Settings\xxx\My
    Documents\Certi ficate.cer")

    myHttpWebReques t.ClientCertifi cates.Add(cert)

    Dim newStream As Stream = myHttpWebReques t.GetRequestStr eam()

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



    ' Sends the request and waits for a response.

    myHttpWebRespon se = CType(myHttpWeb Request.GetResp onse(), HttpWebResponse )

    Dim sr As New StreamReader(my HttpWebResponse .GetResponseStr eam)

    Dim res As String = sr.ReadToEnd

    Me.TextBox1.Tex t = res

    End Sub


Working...