Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
914 views
in Technique[技术] by (71.8m points)

automate submitting a post form that is on a website with vba and xmlhttp

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

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is nothing wrong with the code. :) I tested it and it works fine. The error might be somewhere else.

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

Regards

Sid


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...