vba Xmlhttp 请求引发拒绝访问错误

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

Xmlhttp request is raising an Access Denied error

vbaxmlhttprequest

提问by Matteo NNZ

The following Excel macro, which is making an xmlhttp request to this webpage to retrieve some values at a second stage, has worked normally in VBA until some time ago:

以下 Excel 宏向此网页发出 xmlhttp 请求以在第二阶段检索某些值,直到一段时间之前在 VBA 中都可以正常工作:

Sub WebReq()

Link = "http://it.finance.yahoo.com/q?s=^FCHI&ql=10" & str(rnd())
Set htm = CreateObject("htmlFile")

Set RequestWeb = CreateObject("msxml2.xmlhttp")
With RequestWeb
    .Open "GET", "" & Link & "", False
    .send
    htm.body.innerhtml = .responsetext
End With

End Sub

Now, instead, at the call of the method:

现在,相反,在调用方法时:

    .send

of the object msxml2.xmlhttp is raising the following error:

对象 msxml2.xmlhttp 引发以下错误:

Run-time error '-2147024891 (80070005)'
Access is denied. 

I've been looking on the web but all the similar threads are never answered. Can anyone explain me what this error means, and if there's any way I could fix it or even just work around it?

我一直在网上寻找,但从未回答过所有类似的问题。任何人都可以向我解释这个错误是什么意思,如果有什么办法可以修复它,甚至只是解决它?

Note: the random string at the end of the variable 'Link' has been added to force the page reloading, since the script is retrieving real-time values and so it should be loaded every time.

注意:变量“Link”末尾的随机字符串已被添加以强制页面重新加载,因为脚本正在检索实时值,因此每次都应加载。

Additional information: while looking for a solution, I'm noticing now that the random part of the link is yielding always the same value even when I end the running and restart again:

附加信息:在寻找解决方案时,我现在注意到,即使我结束运行并再次重新启动,链接的随机部分也始终产生相同的值:

Link = http://it.finance.yahoo.com/q?s=^FCHI&ql=10 .7055475

Why is this happening? Shouldn't rnd()yield a new random value between 0 and 1 at every call?

为什么会这样?不应该rnd()在每次调用时产生 0 到 1 之间的新随机值吗?

回答by John Lasschuit

Use

CreateObject("MSXML2.ServerXMLHTTP.6.0") 

The standard request fired from a local machine forbids access to sites that aren't trusted by IE. MSXML2.ServerXMLHTTP.6.0is the server-sideobject, that doesn't perform those checks.

从本地计算机发出的标准请求禁止访问不受 IE 信任的站点。MSXML2.ServerXMLHTTP.6.0服务器端对象,不执行这些检查。

回答by Don

i found that, in my case, changing http to https fixed the access denied problem. i can only assume that the website somehow made a change and didn't tell anyone

我发现,就我而言,将 http 更改为 https 修复了访问被拒绝的问题。我只能假设该网站以某种方式进行了更改并且没有告诉任何人

回答by Saad A

access denied is IE issue

访问被拒绝是 IE 问题

internet options > security tab > custom security level > Miscellaneous >Access data sources across domains > enable

回答by Rich

Update

更新

Sub WebReq()

link = "http://it.finance.yahoo.com/q?s=^FCHI&ql=10" & Str(Rnd())
Set htm = CreateObject("htmlFile")
Dim objHttp

    Set objHttp = CreateObject("Msxml2.ServerXMLHTTP")
    objHttp.Open "GET", link, False

    objHttp.Send
    htm.body.innerhtml = objHttp.responsetext
    Set objHttp = Nothing
End Sub

回答by Apostolos

This works for me:

这对我有用:

With CreateObject("MSXML2.ServerXMLHTTP.6.0")
  .Open "GET", URL, False
  .Send
  content = .ResponseText
End With

回答by user8635117

I was able to fix it by changing the link being passed from being "http://" to "https://"

我能够通过将传递的链接从“http://”更改为“https://”来修复它

The site I was pulling had upgraded and trying to pull the data using the unsecured link was failing. Works great now (no code change required.

我正在拉取的站点已升级,尝试使用不安全链接拉取数据失败。现在效果很好(无需更改代码。

回答by AdamsTips

In my case the user didn't have AD permissions to the proxy server on our corporate network. (Simple oversight when setting up the user.) Adding the missing security group fixed the problem for the user.

在我的情况下,用户对我们公司网络上的代理服务器没有 AD 权限。(设置用户时的简单疏忽。)添加缺少的安全组为用户解决了问题。

回答by Shaz cool

add "www" after "https://" in your custom Link, Like this:

在您的自定义链接中的“https://”之后添加“www”,如下所示:

XMLPage.Open "GET", "https://www.x-rates.com/table/?from=GBP&amount=3", False    
XMLPage.send

回答by BrupieD

I'm afraid I don't understand exactly why this problem occurs, but I'm guessing it is the secure "https://" versus insecure "http://". I ran into the same "access denied" message while following sample code from a VBA course. The original code was:

恐怕我不明白为什么会出现这个问题,但我猜是安全的“https://”与不安全的“http://”。在遵循 VBA 课程中的示例代码时,我遇到了相同的“访问被拒绝”消息。原来的代码是:

XMLPage.Open "GET", "http://x-rates.com/table/?from=GBP&amount=3", False    
XMLPage.send

I changed the "http://" to "https://" and the error went away.

我将“http://”更改为“https://”,错误消失了。