【问题标题】:automate submitting a post form that is on a website with vba and xmlhttp使用 vba 和 xmlhttp 自动提交网站上的帖子表单
【发布时间】:2012-02-06 14:12:59
【问题描述】:

我在 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

【问题讨论】:

    标签: forms vba excel-2010 xmlhttprequest


    【解决方案1】:

    另一个answer 的变体适用于我,无需 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
    

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


    ...和late-bound 版本:

    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
    

    【讨论】:

      【解决方案2】:

      代码没有问题。 :) 我测试了它,它工作正常。错误可能在其他地方。

      我只是稍微调整了代码以使用 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
      

      问候

      席德

      【讨论】:

        猜你喜欢
        • 2016-03-25
        • 2013-07-23
        • 1970-01-01
        • 2012-02-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多