Pass Parameters in VBA HTTP Post Request

元气小坏坏 提交于 2019-12-23 11:56:10

问题


I'm trying to do a simple post request on the main search bar of http://forums.egullet.org/. (This is one example, but I'm trying to build a tool that will work with many.)

The problem is that I can't seem to figure out the right way to structure/place the parameters such that the server processes my request. (I do get a response, but it's just a page asking me to try the search again, rather than the result I get when I do the search in a browser. The argument string was pulled straight out of firebug, so I'm fairly sure that it's correct. I just get the impression that i'm not putting it in the right place/structuring it correctly/saying everything that I need to, but I don't know what to change. It's worth noting that I previously had this working by editing the DOM of an internet explorer object, but I'm trying to switch to XMLHTTP because it's much faster/more reliable. Thanks for your help!

Sub httpPost()
Dim XMLHTTP
Dim result As String
Dim argumentString
argumentString = "?search_term=eggs&search_app=forums"
Set XMLHTTP = CreateObject("MSXML2.XMLHTTP.6.0")
XMLHTTP.Open "POST", _
    "http://forums.egullet.org/index.php?app=core&module=search&do=search&fromMainBar=1", False
XMLHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
XMLHTTP.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
XMLHTTP.send argumentString
result = XMLHTTP.responsetext
Set XMLHTTP = Nothing
End Sub

回答1:


I think you need an ampersand where you have a question mark

argumentString = "&search_term=eggs&search_app=forums"



回答2:


To make it more concise and get the title of that target page:

Sub httpPost()
    Dim http As New XMLHTTP60, html As New HTMLDocument
    Dim post As Object, argstr As String

    argstr = "type=all&q=eggs"

    With http
        .Open "POST", "https://forums.egullet.org/search/?", False
        .setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"
        .setRequestHeader "Content-type", "application/x-www-form-urlencoded"
        .send argstr
        html.body.innerHTML = .responseText
    End With

    For Each post In html.getElementsByClassName("ipsStreamItem_title")
        With post.getElementsByTagName("a")
            If .Length Then Row = Row + 1: Cells(Row, 1) = .Item(0).innerText
        End With
    Next post
End Sub


来源:https://stackoverflow.com/questions/18111884/pass-parameters-in-vba-http-post-request

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