POST request created in vba brings back nothing as result

被刻印的时光 ゝ 提交于 2020-01-16 08:36:43

问题


I've written a very tiny script in vba using POST request. However, when I run it, I get nothing as result except for a blank message. I've tried to fill in the request parameter accordingly. Perhaps, I can't notice which should be included in the parameter. The page I'm dealing with contains several images in it's right panel. When an image is clicked the request about which i'm talking here is sent to the server and brings back the result and displays new information concerning its' flavor under it. My goal is to parse all the flavors connected to each images. Anyways, I'm trying to attach all the things necessary to find out what i'm missing. Thanks in advance.

This is what I got from chrome developer tools to prepare the POST request: "https://www.dropbox.com/s/zjn0ahixhu58miq/RequestStatus.txt?dl=0"

Here is what I'm trying with:

Sub PostReq()

    Dim http As New XMLHTTP60, html As New HTMLDocument
    Dim ArgumentStr As String

    ArgumentStr = "opt=flavor&opt1=207&opt2=47&ip=105"
    With http
        .Open "POST", "https://www.optigura.com/product/ajax/details.php", 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"
        .setRequestHeader "Accept", "application/json, text/javascript, */*; q=0.01"
        .send ArgumentStr
        html.body.innerHTML = .responseText
    End With

    MsgBox http.responseText

End Sub

This is the original link to the webpage:

"https://www.optigura.com/uk/product/gold-standard-100-whey/"


回答1:


Your code sets a request header like so:

.setRequestHeader "Content-type", "application/x-www-form-urlencoded"

so the script is going to expect the argument string to be URL encoded (which it isn't).

Try either encoding the string, or send the request using "GET" instead.




回答2:


Finally, I've made it. To receive the required response it is necessary to send a GET request first then again send a POST request using the response from that get request. Here is the working one:

Sub httpPost()

    Dim http As New XMLHTTP60, html As New HTMLDocument
    Dim ArgumentStr As String

    ArgumentStr = "opt=flavor&opt1=207&opt2=47&ip=105"

    With http
        .Open "GET", "https://www.optigura.com/uk/product/gold-standard-100-whey/", False
        .send
    End With

    With http
        .Open "POST", "https://www.optigura.com/product/ajax/details.php", False
        .setRequestHeader "X-Requested-With", "XMLHttpRequest"
        .setRequestHeader "Content-type", "application/x-www-form-urlencoded"
        .setRequestHeader "Accept", "application/json, text/javascript, */*; q=0.01"
        .send ArgumentStr
        html.body.innerHTML = .responseText
    End With

    MsgBox http.responseText

End Sub


来源:https://stackoverflow.com/questions/45416258/post-request-created-in-vba-brings-back-nothing-as-result

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