0

I have created Soap service, using WSDL file. While testing on SOAP UI (XML part with passing header and client id,secret) it is giving proper response. But through service gives error

The request was aborted: Could not create SSL/TLS secure channel

Can someone analise code my service look like :

    Dim Request As WebRequest
    Dim Response As WebResponse
    Dim DataStream As Stream
    Dim Reader As StreamReader
    Dim SoapByte() As Byte
    Dim pSuccess As Boolean = True
    Dim SD2Request As String

    SoapByte = System.Text.Encoding.UTF8.GetBytes(MyXMLString)

        Dim myCred As System.Net.CredentialCache = New System.Net.CredentialCache()
        Dim netCred As NetworkCredential = New NetworkCredential("testclient", "test@123")
        myCred.Add(New Uri("https://URL"), "Basic", netCred)
        Request = WebRequest.Create("https://URL")
        Request.Headers.Add("Client-ID", "ID-1234")
        Request.Headers.Add("Client-Secret", "Secret-1234")
        Request.PreAuthenticate = True
        Request.ContentType = "application/xml; charset=utf-8"
        Request.ContentLength = SoapByte.Length
        Request.Method = "POST"
        Request.Credentials = myCred
        DataStream = Request.GetRequestStream()
        DataStream.Write(SoapByte, 0, SoapByte.Length)
        DataStream.Close()
        Response = Request.GetResponse()
        DataStream = Response.GetResponseStream()
        Reader = New StreamReader(DataStream)
        SD2Request = Reader.ReadToEnd()
        DataStream.Close()
        Reader.Close()
        Response.Close()
        Return SD2Request

I have imported WSDL file as service reference.

1 Answers1

0

.NET 4.5 hasn't enabled TLS by default. You've got to enable it manually by setting the System.Net.ServicePointManager.SecurityProtocol property:

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls Or SecurityProtocolType.Tls11 Or SecurityProtocolType.Tls12
Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
  • I have already tried passing security protocol manually and change of target framework, but error remains same. – Lokesh Chinchkhedkar Mar 16 '18 at 10:28
  • @LokeshChinchkhedkar : Have you verified that it isn't the web site returning HTTP code 401? See: https://stackoverflow.com/a/25391935 – Visual Vincent Mar 16 '18 at 10:55
  • I have checked service, it is returning 200 as response. I think issue is with auth and client id and secret passing. Is any other way to pass it, since error comes on Iine: Request.GetRequestStream(). – Lokesh Chinchkhedkar Mar 19 '18 at 05:03
  • @LokeshChinchkhedkar : The error occurs because it's not being able to establish a proper HTTPS connection. It has nothing to do with the data you send to the server. The problem is that it's hard to identify the cause of this error. It could be anything from an invalid server certificate to an incompatibility in your code (the latter which usually requires minor change, if you can find out what the cause is). – Visual Vincent Mar 19 '18 at 06:31