使用vba和xmlhttp自动提交在网站上发布的表单 [英] automate submitting a post form that is on a website with vba and xmlhttp
问题描述
我在Excel 2010中通过vba使用 xmlhttp
>我需要以编程方式将项目添加到网站上的购物车。我有下面的代码,它使用 POST
方法
I'm using xmlhttp
via vba in excel 2010. I need to programmatically add an item to a shopping cart on a website. I have the code below so far, it uses the POST
method
我认为有两件事是错误的我的代码但不知道如何解决 - 它不显示提交表单的位置。这里是那个网址:
A couple of things I think are wrong with my code but not sure how to fix - It doesn't show where the form is that is being submitted. Here is that url:
http://www.craft-e-corner.com/p-2688-new-testament-cricut-cartridge.aspx
我作为处理表单的URL输入的url是form的action =部分中的url。
The url I entered as the url that processes the form is the url in the "action=" part of "form".
如何验证表单已发布?
How can I verify that the form posted?
Sub post_frm()
Dim xmlhttp As Object
Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")
' Indicate that page that will receive the request and the
' type of request being submitted
xmlhttp.Open "POST", "http://www.craft-e-corner.com/addtocart.aspx?returnurl=showproduct.aspx%3fProductID%3d2688%26SEName%3dnew-testament-cricut-cartridge", False
' Indicate that the body of the request contains form data
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
' Send the data as name/value pairs
xmlhttp.send "Quantity=1&VariantID=2705&ProductID=2688"
Set xmlhttp = Nothing
End Sub
推荐答案
代码没有任何问题。 :)我测试了它,它工作正常。这个错误可能在其他地方。
There is nothing wrong with the code. :) I tested it and it works fine. The error might be somewhere else.
我只是稍微调整了代码来使用IE来测试输出,它现在工作得很好:)我已经在Excel中测试过了2007年目前。很快将在2010年进行测试。顺便说一句,你使用的是IE版本?
I just tweaked the code little bit to use IE to test the output and it works just fine now :) I have tested it in Excel 2007 at the moment. Will test it in 2010 shortly. BTW which version of IE are you using?
这是我测试过的代码,它工作得很好。
Here is the code that I tested and it works just fine.
Option Explicit
Sub post_frm()
Dim objIE As Object, xmlhttp As Object
Dim response As String
Set objIE = CreateObject("InternetExplorer.Application")
objIE.navigate "about:blank"
objIE.Visible = True
Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")
'~~> Indicates that page that will receive the request and the type of request being submitted
xmlhttp.Open "POST", "http://www.craft-e-corner.com/addtocart.aspx?returnurl=showproduct.aspx%3fProductID%3d2688%26SEName%3dnew-testament-cricut-cartridge", False
'~~> Indicate that the body of the request contains form data
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
'~~> Send the data as name/value pairs
xmlhttp.Send "Quantity=1&VariantID=2705&ProductID=2688"
response = xmlhttp.responseText
objIE.document.Write response
Set xmlhttp = Nothing
End Sub
问候
Sid
这篇关于使用vba和xmlhttp自动提交在网站上发布的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!