Question Web client cannot access secured web service

groadsvb

Well-known member
Joined
Nov 13, 2006
Messages
75
Programming Experience
Beginner
My client that uses the httpwebrequest class to access a service cannot access the service. I was provided a certificate which I installed on the computer the web client will run on. My request needs to provide a username and password which I do but I still get the following message: "The underlying connection was closed. Could not establish trust relationship for the SSL/TLS secure channel" Thanks.

Below is snippet of code:
 Dim cert As System.Security.Cryptography.X509Certificates.X509Certificate
        cert = System.Security.Cryptography.X509Certificates.X509Certificate.CreateFromCertFile("c:\was33.cer")

        Dim defaultUri As String = String.Empty
        defaultUri = "https://111.111.11.11:999/services/queryservice"
        Dim query As String = String.Empty
        Dim urii As String = String.Empty
        Dim postData As String = String.Empty


        Dim responseData As String = String.Empty
        Dim hwrequest As System.Net.HttpWebRequest
       
       urii = defaultUri
       
        hwrequest = System.Net.HttpWebRequest.Create(urii)
        hwrequest.Method = "POST"
        hwrequest.Accept = "gzip, deflate"
        hwrequest.ContentType = "text/xml; charset=utf-8"
        hwrequest.Timeout = 20000

        hwrequest.Credentials = New System.Net.NetworkCredential("psc8", "psw8")

        hwrequest.ClientCertificates.Add(cert)

        If Not String.IsNullOrEmpty(txtQuery.Text) Then
            postData = txtQuery.Text
        End If

        Dim encoding As New Text.ASCIIEncoding() 'Use UTF8Encoding for XML requests
        Dim postByteArray() As Byte = encoding.GetBytes(postData)

        hwrequest.ContentLength = postByteArray.Length
        Try
            Dim postStream As IO.Stream = hwrequest.GetRequestStream()
            postStream.Write(postByteArray, 0, postByteArray.Length)
            postStream.Close()
        Catch ex As Exception
            txtReply.Text = ex.Message
            Exit Sub
        End Try

        Dim hwresponse As Net.HttpWebResponse = hwrequest.GetResponse()
        Try

            If hwresponse.StatusCode = Net.HttpStatusCode.OK Then
                Dim responseStream As IO.StreamReader = _
                  New IO.StreamReader(hwresponse.GetResponseStream())
                responseData = responseStream.ReadToEnd()
            End If

        Catch ex As Exception
            responseData = ex.Message.ToString
        End Try
        hwresponse.Close()
 
Last edited by a moderator:
Top Bottom