VB6 中的 SecurityProtocolType.Tls12
SecurityProtocolType.Tls12 in VB6
我想与仅支持 TLS 1.2 及更高版本的服务器建立安全连接。
我将以下行添加到我的 VB.NET 应用程序以启用与我的 https 服务器的通信:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
不过,我还有一些VB6应用也需要使用https服务器。
并且以上行不适用于 VB6,并且代码抛出“安全通道支持中发生错误”。如果我在 http 而不是 https 上使用它,它工作正常。
我怎样才能为 VB6 实现同样的效果?
谢谢!
顺便说一句,这是我的代码:
Public Function GetHTTPResponse(Byval uSomeInput) As String
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 'required in order to use https!!!
Dim nReq As Net.WebRequest = Net.WebRequest.Create("https://domain.xyz/answer.php")
nReq.Method = "Post"
nReq.ContentType = "application/x-www-form-urlencoded"
Dim nReqStream As IO.Stream = nReq.GetRequestStream()
Dim nASCIIEncoding As New System.Text.ASCIIEncoding
Dim btPostData As Byte() = Nothing
btPostData = nASCIIEncoding.GetBytes("&MyPHPInput=" & uSomeInput)
nReqStream.Write(btPostData, 0, btPostData.Length)
nReqStream.Close()
Dim reader As New IO.StreamReader(nReq.GetResponse().GetResponseStream())
Dim sRet As String = reader.ReadToEnd
Return sRet
End Function
这与 VB6 中的代码相同,但在 VB6 中,我不能(据我所知)不设置 Tls12 安全协议:
Public Function GetHTTPResponse(ByVal uSomeInput) As String
'How do I set Tls12 protocol here??
Dim xmlhttp As Object
Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")
xmlhttp.Open "POST", "https://domain.xyz", False
xmlhttp.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded" & VBA.Chr(10) & VBA.Chr(13)
xmlhttp.Send pConvertStringToByte("https://domain.xyz/answer.php?MyPHPInput=" & uSomeInput)
GetHTTPResponse = xmlhttp.responseText
End Function
编辑:
我已按照建议尝试使用 WinHttp.WinHttpRequest.5.1,而不是讨论 here.
它似乎有效,但由于某种原因,变量没有被脚本传输/识别,而我的旧方法使用 MSXML2.ServerXMLHTTP.
识别它们
这是我的代码:
Public Function GetHTTPResponse(ByVal uSomeInput) As String
Dim xmlhttp As Object
Set xmlhttp = CreateObject("WinHttp.WinHttpRequest.5.1")
'force TLS 1.2
xmlhttp.Option(9) = 2048
xmlhttp.Option(6) = True
xmlhttp.Open "POST", "https://domain.xyz", False
xmlhttp.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded" & VBA.Chr(10) & VBA.Chr(13)
xmlhttp.Send pConvertStringToByte("https://domain.xyz/answer.php?MyPHPInput=" & uSomeInput)
GetHTTPResponse = xmlhttp.responseText
End Function
有人知道为什么我的旧字节数组不被接受为“WinHttp.WinHttpRequest.5.1”而它适用于“MSXML2.ServerXMLHTTP”吗?
试试这个:
' Define uSomeInput's data type, otherwise it's passed as a Variant, which might or might not be what we intended
Public Function GetHTTPResponse(ByVal uSomeInput As String) As String
' Use early instead of late binding
Dim xmlhttp As WinHttp.WinHttpRequest
Set xmlhttp = New WinHttpRequest
' force TLS 1.2
xmlhttp.Option(9) = 2048
xmlhttp.Option(6) = True
xmlhttp.Open "POST", "https://domain.xyz", False
xmlhttp.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded" & vbNewLine
' Let the webserver know the size of the data
xmlhttp.SetRequestHeader "Content-Length", CStr(LenB(uSomeInput)) & vbNewLine
xmlhttp.Send pConvertStringToByte("https://domain.xyz/answer.php?MyPHPInput=" & uSomeInput)
' Not sure why the string to byte array conversion is necessary
' Could just try the string as well
xmlhttp.Send "https://domain.xyz/answer.php?MyPHPInput=" & uSomeInput
GetHTTPResponse = xmlhttp.ResponseText
End Function
如果其他一切都失败了,并且您需要 VB6 解决方案,因此愿意(或被迫) 花钱($ 395)在商业组件上,看看 SocketTools。虽然很诱人,但我建议使用 Library 而不是 ActiveX 版本 (它只是前者的包装,恕我直言)。过去我在 VB6 中使用 SocketTools 取得了巨大的成功 (自其 6.x 版本以来,IIRC)。我开始使用它,因为我面临着与您非常相似的任务:我必须在我的 VB6 应用程序中支持 SFTP 传输。
我想与仅支持 TLS 1.2 及更高版本的服务器建立安全连接。
我将以下行添加到我的 VB.NET 应用程序以启用与我的 https 服务器的通信:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
不过,我还有一些VB6应用也需要使用https服务器。
并且以上行不适用于 VB6,并且代码抛出“安全通道支持中发生错误”。如果我在 http 而不是 https 上使用它,它工作正常。
我怎样才能为 VB6 实现同样的效果?
谢谢!
顺便说一句,这是我的代码:
Public Function GetHTTPResponse(Byval uSomeInput) As String
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 'required in order to use https!!!
Dim nReq As Net.WebRequest = Net.WebRequest.Create("https://domain.xyz/answer.php")
nReq.Method = "Post"
nReq.ContentType = "application/x-www-form-urlencoded"
Dim nReqStream As IO.Stream = nReq.GetRequestStream()
Dim nASCIIEncoding As New System.Text.ASCIIEncoding
Dim btPostData As Byte() = Nothing
btPostData = nASCIIEncoding.GetBytes("&MyPHPInput=" & uSomeInput)
nReqStream.Write(btPostData, 0, btPostData.Length)
nReqStream.Close()
Dim reader As New IO.StreamReader(nReq.GetResponse().GetResponseStream())
Dim sRet As String = reader.ReadToEnd
Return sRet
End Function
这与 VB6 中的代码相同,但在 VB6 中,我不能(据我所知)不设置 Tls12 安全协议:
Public Function GetHTTPResponse(ByVal uSomeInput) As String
'How do I set Tls12 protocol here??
Dim xmlhttp As Object
Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")
xmlhttp.Open "POST", "https://domain.xyz", False
xmlhttp.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded" & VBA.Chr(10) & VBA.Chr(13)
xmlhttp.Send pConvertStringToByte("https://domain.xyz/answer.php?MyPHPInput=" & uSomeInput)
GetHTTPResponse = xmlhttp.responseText
End Function
编辑:
我已按照建议尝试使用 WinHttp.WinHttpRequest.5.1,而不是讨论 here.
它似乎有效,但由于某种原因,变量没有被脚本传输/识别,而我的旧方法使用 MSXML2.ServerXMLHTTP.
识别它们这是我的代码:
Public Function GetHTTPResponse(ByVal uSomeInput) As String
Dim xmlhttp As Object
Set xmlhttp = CreateObject("WinHttp.WinHttpRequest.5.1")
'force TLS 1.2
xmlhttp.Option(9) = 2048
xmlhttp.Option(6) = True
xmlhttp.Open "POST", "https://domain.xyz", False
xmlhttp.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded" & VBA.Chr(10) & VBA.Chr(13)
xmlhttp.Send pConvertStringToByte("https://domain.xyz/answer.php?MyPHPInput=" & uSomeInput)
GetHTTPResponse = xmlhttp.responseText
End Function
有人知道为什么我的旧字节数组不被接受为“WinHttp.WinHttpRequest.5.1”而它适用于“MSXML2.ServerXMLHTTP”吗?
试试这个:
' Define uSomeInput's data type, otherwise it's passed as a Variant, which might or might not be what we intended
Public Function GetHTTPResponse(ByVal uSomeInput As String) As String
' Use early instead of late binding
Dim xmlhttp As WinHttp.WinHttpRequest
Set xmlhttp = New WinHttpRequest
' force TLS 1.2
xmlhttp.Option(9) = 2048
xmlhttp.Option(6) = True
xmlhttp.Open "POST", "https://domain.xyz", False
xmlhttp.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded" & vbNewLine
' Let the webserver know the size of the data
xmlhttp.SetRequestHeader "Content-Length", CStr(LenB(uSomeInput)) & vbNewLine
xmlhttp.Send pConvertStringToByte("https://domain.xyz/answer.php?MyPHPInput=" & uSomeInput)
' Not sure why the string to byte array conversion is necessary
' Could just try the string as well
xmlhttp.Send "https://domain.xyz/answer.php?MyPHPInput=" & uSomeInput
GetHTTPResponse = xmlhttp.ResponseText
End Function
如果其他一切都失败了,并且您需要 VB6 解决方案,因此愿意(或被迫) 花钱($ 395)在商业组件上,看看 SocketTools。虽然很诱人,但我建议使用 Library 而不是 ActiveX 版本 (它只是前者的包装,恕我直言)。过去我在 VB6 中使用 SocketTools 取得了巨大的成功 (自其 6.x 版本以来,IIRC)。我开始使用它,因为我面临着与您非常相似的任务:我必须在我的 VB6 应用程序中支持 SFTP 传输。