本文介绍了Http Post在Vba的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试图找出如何在VBA中进行POST。
理想情况下,我正在寻找一个简单的工作示例,我可以玩。
这是我到目前为止,但我不确定该怎么做。大部分formdata是什么样子。

 函数WinHTTPPostRequest(URL,formdata,Boundary)
Dim http

设置http = CreateObject(MSXML2.XMLHTTP)

http.OpenPOST,URL,False

'设置Content-Type头'
http.setRequestHeaderContent-Type,multipart / form-data; boundary =+ Boundary

'发送表单数据到URL作为POST二进制请求'
http。发送formdata

'获取已收到上传的脚本的结果'
WinHTTPPostRequest = http.responseText
结束函数

编辑:



所以我安装了firebug,以便我可以获得formdata的对象名称(见代码)。我会认为formdata看起来像这样Form1 = A& Form2 = B。但它仍然没有奏效。任何关于我应该如何做这个更好的建议?



编辑:
所以似乎可能有隐藏的字段,我需要发送在我的POST请求。

解决方案

要以您建议的格式发送表单数据(即与GET请求相同),我相信您需要设置Content-Type标题为application / x-www-form-urlencoded。



如果您需要发送更复杂的数据(例如,包括文件上传或其他二进制文件数据),您可能会更好地将Content-Type设置为multipart / form-data。有关如何格式化请求正文的详细信息,请参见,但您可以找到一个可以为你做的这个图书馆会更好。将格式化完全正确可能非常棘手,除非您将其作为学习体验,否则无需重新设计轮廓。


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.

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?

Edit:So it seems there might be hidden fields that I need to send in my POST request.

解决方案

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".

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.

这篇关于Http Post在Vba的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 17:45