HTTPRequest.SetClientCertificate

HTTPRequest.SetClientCertificate

注意 - 我是加密主题的新手。 由于技术限制,我必须 Excel 和 VBA.

我需要通过 TLS1.2 进行 HTTP 调用。 我只是找不到将我的证书作为字符串传递给 httpSender.SetClientCertificatestring 方法的正确形式。

感谢任何帮助。

p.s - 证书存储在证书存储中,而不是注册表中。

这是我的代码:

Public Sub restApiCallV2()

' taken from: https://markohoven.com/2020/03/06/msxml2-serverxmlhttp-and-tls1-2/

Dim httpSender As Object
Dim strUrl As String
Dim blnAsync As Boolean
Dim strResponse As String
Dim jsonResponse As Object

strUrl = "https://jsonplaceholder.typicode.com/todos/2"
blnAsync = True

On Error GoTo eh
Set httpSender = New WinHttp.WinHttpRequest

'force TLS 1.2
httpSender.Option(WinHttpRequestOption_SecureProtocols) = SecureProtocol_TLS1_2
httpSender.Option(WinHttpRequestOption_EnableRedirects) = True


call httpSender.SetClientCertificate("my certificate string here")
httpSender.Open "GET", strUrl, False
httpSender.setRequestHeader "User-Agent", "My App V1.0"
httpSender.setRequestHeader "Content-type", "application/json"

Debug.Print ("Calling...")

httpSender.send ("") ' if get call, uses the URL in the open command
Failure = (httpSender.Status <> 200)
If Not Failure Then
    strResponse = httpSender.responseText
Else
    Call Err.Raise(5000, "start_here.restApiCallV2", "Failure: received status : " & XMLServer.Status)
End If


Debug.Print (strResponse)

Debug.Print ("Completed!")
Exit Sub
eh:
    Debug.Print ("Error!")
    Debug.Print ("Number: " & Err.Number & ", Source: " & Err.Source & ", Description: " & Err.Description)

End Sub

我的同事能够帮助我,这对我们有用: 调用 httpSender.SetClientCertificate("CURRENT_USER\name_of_certificate").

我假设您可以使用 ROOT 和 CA 而不是 CURRENT_USER。