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

本秂侑毒 提交于 2019-11-28 22:09:37
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?

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

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!