vba Excel VBA中的XmlHttp Post不更新网站表单

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

XmlHttp Post in Excel VBA not updating website form

vbaexcel-vbaxmlhttprequestexcel-2010msxml

提问by hr2d33

I routinely have to search the state of NV for unclaimed property and put the results in an Excel spreadsheet. I am trying to automate the process but I'm limited to using Excel 2010 and VBA. Below is the URL to the site I'm trying to submit a form using xmlhttp.

我通常必须在 NV 的状态中搜索无人认领的财产,并将结果放入 Excel 电子表格中。我正在尝试自动化该过程,但仅限于使用 Excel 2010 和 VBA。下面是我尝试使用 xmlhttp 提交表单的站点的 URL。

URL: https://nevadatreasurer.gov/UPSearch/

网址:https: //nevadatreasurer.gov/UPSearch/

I created a class to automate submitting forms on other websites but no matter what I enter in the postdata the form is never submitted. Below is my submission, and method to submit the form.

我创建了一个类来自动在其他网站上提交表单,但无论我在 postdata 中输入什么,表单都不会提交。下面是我的提交,以及提交表单的方法。

Call to class:

打电话上课:

cXML.openWebsite "Post", "https://nevadatreasurer.gov/UPSearch/Index.aspx", _
                 "ctl04$txtOwner=" & strSearchName

Class method:

类方法:

Public Sub openWebsite(strOpenMethod As String, strURL As String, _
Optional strPostData As String)

pXmlHttp.Open strOpenMethod, strURL


If strPostData <> "" Then
    strPostData = convertSpaceToPlus(strPostData)
    pXmlHttp.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
    pXmlHttp.send (strPostData)
Else
    pXmlHttp.send
End If

'Create DOM html documnet
pHtmlObj.body.innerHTML = pXmlHttp.responseText

End Sub

Each time the responseTextis the main website with no updates, as if I submitted no postdata. I'm fairly new to IE automation but can someone provide a reason why this isn't working and a code example that works?

每次responseText都是没有更新的主网站,就好像我没有提交任何postdata。我对 IE 自动化相当陌生,但有人可以提供一个为什么这不起作用的原因以及一个有效的代码示例吗?

Thanks!

谢谢!

Update: 7/26/13 8:30am PST

更新:太平洋标准时间 7/26/13 上午 8:30

Without any changes to my method I was able to submit forms through another website. The state of OR unclaimed property form. It worked perfect!

我的方法没有任何改变,我就可以通过另一个网站提交表格。或无人认领的财产状况表格。它工作完美!

URL: https://oregonup.us/upweb/up/UP_search.asp

网址:https: //oregonup.us/upweb/up/UP_search.asp

However I ran into the same problem when I tried the state of CA unclaimed property website. No matter what I do, the responseTextis always the original search page with no update.

然而,当我尝试 CA 无人认领财产网站的状态时,我遇到了同样的问题。无论我做什么,responseText始终是没有更新的原始搜索页面。

URL: https://scoweb.sco.ca.gov/UCP/Default.aspx

网址:https: //scoweb.sco.ca.gov/UCP/Default.aspx

It still does not work with the state of NV on my original post. I am using the proper post data, URL encoded for each website and can see no difference. Any help would be appreciated.

它仍然不适用于我原始帖子中的 NV 状态。我正在使用正确的帖子数据,为每个网站编码的 URL,看不出有什么区别。任何帮助,将不胜感激。

回答by Santosh

Try below code

试试下面的代码

Public Sub openWebsite(strOpenMethod As String, strURL As String, Optional strPostData As String)

    Dim pXmlHttp As Object
    Set pXmlHttp = CreateObject("MSXML2.XMLHTTP")
    pXmlHttp.Open strOpenMethod, strURL, False
    pXmlHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    pXmlHttp.send (strPostData)


    Dim pHtmlObj As Object
    Set pHtmlObj = CreateObject("htmlfile")
    pHtmlObj.body.innerHTML = pXmlHttp.ResponseText
    MsgBox pXmlHttp.ResponseText

End Sub

Sub test()
    Dim btnSearch As String, strSearchType As String, strSearchName As String, PostData As String
    btnSearch = "Search"
    strSearchType = "Owner"
    strSearchName = "Santosh"
    PostData = "ctl04%24txtOwner=" & strSearchName & "&ctl04%24btnSearch=" & btnSearch & "&ctl04%24rblSearchType=" & strSearchType
    openWebsite "POST", "https://nevadatreasurer.gov/UPSearch/Index.aspx", PostData
End Sub

Post Data view using Firebug

使用 Firebug 发布数据视图

enter image description here

在此处输入图片说明

URL encode

网址编码

enter image description here

在此处输入图片说明

ResponeText

响应文本

enter image description here

在此处输入图片说明

回答by Andy G

You should urlencode the characters in this string:

您应该对此字符串中的字符进行 urlencode:

"ctl04$txtOwner=" & strSearchName

Ways to do this are discussed here: SO link, as VBA doesn't have a built-in function for this.

此处讨论了执行此操作的方法:SO link,因为 VBA 没有为此提供内置函数。

The dollar sign needs to be replaced with %24 and any spaces with %20. If these are the only non-alphanumeric characters in the string they you could take a simple approach, using VBA.Replace()(twice). You are currently replacing spaces with '+' which will usually work, but the dollar-sign may be an issue.

美元符号需要替换为 %24,任何空格都需要替换为 %20。如果这些是字符串中唯一的非字母数字字符,则您可以采用一种简单的方法,使用VBA.Replace()(两次)。您目前正在用“+”替换空格,这通常会起作用,但美元符号可能是一个问题。