vba Vba中的Http Post
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3963475/
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
Http Post in Vba
提问by Ommit
I am trying to figure out how to make a POST in VBA. Ideally I'm looking for a simple working example that I can play with. This is what I have so far, but I'm not really sure what to do with it. Mostly what does the formdata look like.
我想弄清楚如何在 VBA 中进行 POST。理想情况下,我正在寻找一个可以使用的简单工作示例。到目前为止,这是我所拥有的,但我不确定该怎么做。主要是表单数据是什么样的。
Function WinHTTPPostRequest(URL, formdata, Boundary)
Dim http
Set http = CreateObject("MSXML2.XMLHTTP")
http.Open "POST", URL, False
'Set Content-Type header'
http.setRequestHeader "Content-Type", "multipart/form-data; boundary=" + Boundary
'Send the form data To URL As POST binary request'
http.send formdata
'Get a result of the script which has received upload'
WinHTTPPostRequest = http.responseText
End Function
Edit:
编辑:
So I installed firebug so that I could get the object names for the "formdata" (see code). I would have thought formdata would look something like this "Form1=A&Form2=B". But it's still not working out. Any suggestions on how I should be doing this better?
所以我安装了 firebug 以便我可以获得“formdata”的对象名称(参见代码)。我原以为 formdata 会像这样“Form1=A&Form2=B”。但它仍然没有解决。关于我应该如何做得更好的任何建议?
Edit: So it seems there might be hidden fields that I need to send in my POST request.
编辑:所以似乎我需要在我的 POST 请求中发送隐藏的字段。
回答by eaj
To send form data in the format you suggest (i.e. identical to a GET request), I believe you need to set the Content-Type header to "application/x-www-form-urlencoded".
要以您建议的格式发送表单数据(即与 GET 请求相同),我相信您需要将 Content-Type 标头设置为“application/x-www-form-urlencoded”。
If you need to send more complex data (for example, including file uploads or other binary data), you might be better off setting the Content-Type to "multipart/form-data". The details of how to format the request body are laid out in RFC 2388, but you might be better off finding a library that will do it for you. It can be tricky getting the formatting exactly right, and there's no need to reinvent the wheel unless you're doing it as a learning experience.
如果您需要发送更复杂的数据(例如,包括文件上传或其他二进制数据),最好将 Content-Type 设置为“multipart/form-data”。RFC 2388 中列出了如何格式化请求正文的详细信息,但您最好找到一个可以为您完成此操作的库。使格式完全正确可能很棘手,除非您将其作为学习经验,否则无需重新发明轮子。
回答by AMissico
Download Fiddlerso you can debug/decode the HTTP requests. You might just be missing something simple.
下载Fiddler,以便您可以调试/解码 HTTP 请求。你可能只是错过了一些简单的东西。
Also, there are numerous resuls when searching on "HTTP POST VBA" in MSDN Library. (http://social.msdn.microsoft.com/Search/en-US?query=%2BHTTP%20%2BPOST%20%2BVBA%20-stackoverflow%20-social&ac=8). (Seems to be a "bug" when excluding content, so I excluded stackoverflow and social results in query.)
此外,在 MSDN 库中搜索“HTTP POST VBA”时有很多结果。( http://social.msdn.microsoft.com/Search/en-US?query=%2BHTTP%20%2BPOST%20%2BVBA%20-stackoverflow%20-social&ac=8)。(排除内容时似乎是“错误”,因此我在查询中排除了计算器溢出和社交结果。)
How To Submit Form Data by Using XMLHTTP or ServerXMLHTTP Objectat http://support.microsoft.com/kb/290591which uses VBScript, but is easily converted to vBA.
如何在http://support.microsoft.com/kb/290591 上使用 XMLHTTP 或 ServerXMLHTTP 对象提交表单数据,它使用 VBScript,但很容易转换为 vBA。