使用 vba 和 xmlhttp 自动提交网站上的帖子表单

发布于 2024-12-26 01:13:21 字数 1060 浏览 0 评论 0原文

我在 excel 2010 中通过 vba 使用 xmlhttp。我需要以编程方式将项目添加到网站上的购物车中。到目前为止,我有下面的代码,它使用 POST 方法

我认为我的代码有一些问题,但不确定如何修复 - 它没有显示表单所在的位置已提交。这是该网址:

http://www. craft-e-corner.com/p-2688-new-testament-cricut-cartridge.aspx

我作为处理表单的url输入的url是“action=”部分中的url的“形式”。

如何验证表格是否已发布?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

再见回来 2025-01-02 01:13:21

代码没有任何问题。 :) 我测试了一下,效果很好。错误可能在其他地方。

我只是稍微调整了代码以使用 IE 来测试输出,现在它工作得很好:) 我现在已经在 Excel 2007 中测试了它。很快将在 2010 年进行测试。顺便说一句,您使用的是哪个版本的 IE?

这是我测试过的代码,它工作得很好。

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

问候席

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

动听の歌 2025-01-02 01:13:21

其他答案的变体适用于我,不需要 IE。

Sub Post_HTTP_Form()
'Requires reference to "Microsoft XML, v6.0" or better. (Tools>References)
    Dim msXML As New XMLHTTP60, resp As String
    With msXML
        .Open "POST", "{http://YOUR_URL_HERE.com}", False
        .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
        .Send "{PARAMETER}={VALUE}" 
        resp = StrConv(.responseBody, vbUnicode)
    End With
    Debug.Print resp 'outputs to Immediate Window (CTRL+G to view)
    Set msXML = Nothing
End Sub

只需替换{花括号}中的三个值即可。


...以及后期绑定版本:

Sub Post_HTTP_Form()
    Dim msXML As Object, resp As String
    Set msXML = CreateObject("MSXML2.ServerXMLHTTP")
    With msXML
        .Open "POST", "{http://YOUR_URL_HERE.com}", False
        .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
        .Send "{PARAMETER}={VALUE}" 
        resp = StrConv(.responseBody, vbUnicode)
    End With
    Debug.Print resp 'outputs to Immediate Window (CTRL+G to view)
    Set msXML = Nothing
End Sub

A variation of the other answer works for me without the need for IE.

Sub Post_HTTP_Form()
'Requires reference to "Microsoft XML, v6.0" or better. (Tools>References)
    Dim msXML As New XMLHTTP60, resp As String
    With msXML
        .Open "POST", "{http://YOUR_URL_HERE.com}", False
        .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
        .Send "{PARAMETER}={VALUE}" 
        resp = StrConv(.responseBody, vbUnicode)
    End With
    Debug.Print resp 'outputs to Immediate Window (CTRL+G to view)
    Set msXML = Nothing
End Sub

Just replace the three values in {curly braces}.


...and a late-bound version:

Sub Post_HTTP_Form()
    Dim msXML As Object, resp As String
    Set msXML = CreateObject("MSXML2.ServerXMLHTTP")
    With msXML
        .Open "POST", "{http://YOUR_URL_HERE.com}", False
        .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
        .Send "{PARAMETER}={VALUE}" 
        resp = StrConv(.responseBody, vbUnicode)
    End With
    Debug.Print resp 'outputs to Immediate Window (CTRL+G to view)
    Set msXML = Nothing
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文