我使用 WSDL 文件创建了 Soap 服务。在 SOAP UI(带有传递标头和客户端 ID、SECRET 的 XML 部分(上进行测试时,它会给出正确的响应。但是通过服务给出错误
请求已中止:无法创建 SSL/TLS 安全通道
有人可以分析代码吗我的服务看起来像:
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
我已经导入了WSDL文件作为服务参考。
默认情况下,
.NET 4.5 尚未启用 TLS。您必须通过设置 System.Net.ServicePointManager.SecurityProtocol
属性来手动启用它:
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls Or SecurityProtocolType.Tls11 Or SecurityProtocolType.Tls12