开发者

Http Post in Vba

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.setRequestHeade开发者_开发知识库r "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.


Download Fiddler so you can debug/decode the HTTP requests. You might just be missing something simple.

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

How To Submit Form Data by Using XMLHTTP or ServerXMLHTTP Object at http://support.microsoft.com/kb/290591 which uses VBScript, but is easily converted to vBA.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜