在 VBA HTTP Post 请求中传递参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18111884/
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 22:37:07  来源:igfitidea点击:

Pass Parameters in VBA HTTP Post Request

htmlhttpvbaexcel-vbaxmlhttprequest

提问by ebrts

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

我正在尝试在http://forums.egullet.org/的主搜索栏上做一个简单的帖子请求。(这是一个例子,但我正在尝试构建一种可以与许多人一起使用的工具。)

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!

问题是我似乎无法找出构建/放置参数的正确方法,以便服务器处理我的请求。(我确实收到了响应,但它只是一个页面,要求我再次尝试搜索,而不是我在浏览器中进行搜索时得到的结果。参数字符串是直接从萤火虫中拉出来的,所以我很公平确定它是正确的。我只是觉得我没有把它放在正确的地方/正确地构建它/说出我需要的一切,但我不知道要改变什么。值得注意的是,我以前有这是通过编辑 Internet Explorer 对象的 DOM 来实现的,但我正在尝试切换到 XMLHTTP,因为它更快/更可靠。感谢您的帮助!

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

采纳答案by Dick Kusleika

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

我认为你需要一个&符号,其中有一个问号

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

回答by SIM

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