http client cannot access secured web service


May 23, 2012
My application uses httpwebrequest class to access a secured service. I have received a certificate which I installed on the pc. the web service requires a username and password. I get the following message when running the app:
"The underlying connection was closed. Could not establish trust relationship for the SSL/TLS secure channel"

My code is below but I don't know what is wrong or what else to do. Thanks.

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 = ""
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")


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
Dim postStream As IO.Stream = hwrequest.GetRequestStream()
postStream.Write(postByteArray, 0, postByteArray.Length)
Catch ex As Exception
txtReply.Text = ex.Message
Exit Sub
End Try

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

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
Top Bottom