使用 VBA for Excel 的 HTTPS POST 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1264303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 09:56:31  来源:igfitidea点击:

HTTPS POST request using VBA for Excel

excelvbaposthttps

提问by afewcc

I use "WinHttp.WinHttpRequest.5.1" to send HTTP POST requests from VBA in Excel. But I could not manage to do it for HTTPS, as I received an SSL certificate error.

我使用“WinHttp.WinHttpRequest.5.1”从 Excel 中的 VBA 发送 HTTP POST 请求。但是我无法为 HTTPS 执行此操作,因为我收到了 SSL 证书错误。

What VBA code would you use to negotiate an SSL connection to a website from VBA in Excel ?

您将使用什么 VBA 代码来协商从 Excel 中的 VBA 到网站的 SSL 连接?

采纳答案by Treb

The WinHttpRequestobject has a SetClientCertificatemethod. Try this code example taken from the MSDN(I tried to adapt it for VBA):

所述WinHttpRequest对象具有SetClientCertificate方法。试试这个来自MSDN 的代码示例(我尝试将它改编为 VBA):

' Instantiate a WinHttpRequest object. '
Dim HttpReq as new ActiveXObject("WinHttp.WinHttpRequest.5.1")

' Open an HTTP connection. '
HttpReq.Open("GET", "https://www.test.com/", false)

' Select a client certificate. '
HttpReq.SetClientCertificate("LOCAL_MACHINE\Personal\My Certificate")

' Send the HTTP Request. '
HttpReq.Send()

回答by shahkalpesh

While I have not used the COM component (WinHttpRequest), it seems you need a call to SetClientCertificateprior to calling send, as per the link.

虽然我没有使用 COM 组件 (WinHttpRequest),但 根据链接,您似乎需要在调用 send 之前调用SetClientCertificate

Does that help?

这有帮助吗?

回答by Mr. Bookworm

I have the same situation (send a http request from a VBA in Excel); I created three objects:

我有同样的情况(从 Excel 中的 VBA 发送 http 请求);我创建了三个对象:

Set HttpReq = CreateObject("WinHttp.WinHttpRequest.5.1") 

-- for the http request class, and

-- 对于 http 请求类,以及

Set fsobj = CreateObject("Scripting.FileSystemObject")
Set txtobj = fso.OpenTextFile("C:\PKCERT.PEM")

-- to get into a variable the certificate contents, to pass it to HttpReq.SetClientCertificate,

-- 获取证书内容的变量,将其传递给HttpReq.SetClientCertificate,

certificate_data = txtobj.ReadAll
HttpReq.SetClientCertificate (certificate_content)

So I can send the request including its public key certificate, as usual,

所以我可以像往常一样发送包含其公钥证书的请求,

HttpReq.Send

P.S. I found a script at http://www.808.dk/?code-simplewinhttprequest-- it worked fine in my case, hope in yours too.

PS 我在http://www.808.dk/?code-simplewinhttprequest 上找到了一个脚本——它在我的情况下工作正常,希望你也能。