使用 XMLHTTP 和 Siteminder Secured 站点的 Excel VBA
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18727815/
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
Excel VBA using XMLHTTP with Siteminder Secured site
提问by user2524654
I am completely new to using XMLHTTP and experimenting with trying to download a report off of our company's internet site using Excel VBA. The problem I seem to be running into is that the site is protected by Siteminder. I think I need to use an XMLHTTP.Open with GET but anyway I try all I just seem to get is the Siteminder HTML code. So I am trying to first use a post to send my username and password to Siteminder something like:
我对使用 XMLHTTP 和尝试使用 Excel VBA 从我们公司的网站下载报告完全陌生。我似乎遇到的问题是该站点受 Siteminder 保护。我想我需要将 XMLHTTP.Open 与 GET 一起使用,但无论如何我尝试了所有我似乎得到的只是 Siteminder HTML 代码。因此,我尝试首先使用帖子将我的用户名和密码发送到 Siteminder,例如:
Function PostXmlData(vUrl As String, UserName As String, Password As String, xmlText
As String
Dim XMLHttp As Object
Set XMLHttp = CreateObject("MSXML2.XMLHTTP")
XMLHttp.Open "POST", vUrl, False, UserName, Password
XMLHttp.setRequestHeader "Content-Type", "text/xml;charset=utf-8"
XMLHttp.send (xmlText)
PostXmlData = XMLHttp.responseText
End Function
Sub Posttest ()
Dim add As String
Dim User As String
Dim Pass As String
Dim send As String
Dim ret As Variant
add = "https://mycompanywebsite.com/apps/application/Main/"
User = "username"
Pass = "password"
Send="DashboardId=http://mycompanywebsite.com/DAVCatalog/Dashboards/Teams/Client%20_
Extranet%20AM"
ret = PostXmlData(add, User, Pass, send)
End Sub
Am I on the right track or is this not even possible? Any suggestions would be greatly appreciated or if there is some site someone could direct me to that would be helpful. THanks.
我是在正确的轨道上还是这甚至不可能?任何建议将不胜感激,或者如果有人可以指导我访问某个站点,那将很有帮助。谢谢。
回答by Jaycal
You'll need to load the responseText
into an XMLDocument
so that you can parse it. See example below (make sure you add the Microsoft XML reference to the reference library)
您需要将 加载responseText
到 中,XMLDocument
以便您可以解析它。请参见下面的示例(确保将 Microsoft XML 引用添加到参考库)
Dim xmldoc As MSXML2.DOMDocument
' Create a new XMLDocument to which to load the XML text
Set xmlDoc = New DOMDocument
xmldoc.LoadXML (xmlhttp.responseText)
' Fetch the XML
Set xmlhttp = CreateObject("Microsoft.xmlHTTP")
xmlhttp.Open "Get", yourURL, False
xmlhttp.send
' Create a new XMLDocument to which to load the XML text
Set xmlDoc = New DOMDocument
xmldoc.LoadXML (xmlhttp.responseText)
From here, you should be able to parse the XML using objects like NodeList
, DOMElement
, etc.
从这里,你应该能够解析XML使用对象一样NodeList
,DOMElement
等
回答by 0leg
Could you post the SiteMinder HTML response? Looks like authentication may be failing. Try sending the credentials as a base64 encoded header instead of supplying them to the XMLHttp componet.
你能发布 SiteMinder HTML 响应吗?看起来身份验证可能失败。尝试将凭据作为 base64 编码的标头发送,而不是将它们提供给 XMLHttp 组件。
This is the format.
这是格式。
Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
授权:基本 QWxhZGRpbjpvcGVuIHNlc2FtZQ==
The string after 'Basic' is base64 encoded credentials in the 'id:password' format.
'Basic' 后面的字符串是 'id:password' 格式的 base64 编码凭据。