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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8798661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 14:52:58  来源:igfitidea点击:

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

formsvbaexcel-2010xmlhttprequest

提问by Siddharth Rout

I'm using xmlhttpvia 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 POSTmethod

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

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

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

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

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

回答by Siddharth Rout

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?

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

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

锡德

回答by ashleedawg

A variation of the other answerworks for me without the need for IE.

另一个答案的变体对我有用,不需要 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-boundversion:

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

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