我想从一个网站(只是一个简单的 webrequest)、一个 Excel 中获取数据,唯一的问题是,应用程序位于代理后面,我不想硬编码用户名和密码。
有没有办法获取默认代理并从当前用户设置凭据,而无需对其进行硬编码?
换句话说:我想使用当前登录 Windows 机器的用户对代理进行身份验证。
这是相关代码:
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP.6.0")
URL = "http://stackoverflow.com/"
objHTTP.Open "GET", URL, False
'If possible i would like to get the default proxysettings
objHTTP.setProxy 2, "proxy:8282"
'I dont wont to set this line
objHTTP.setProxyCredentials "username", "password"
objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
objHTTP.send ("")
我用谷歌搜索,但我可以找到一个明确的否或替代解决方案。它应该在 en Windows 环境中运行。任何帮助和/或提示将不胜感激。
最佳答案
目前我找不到更好的解决方案,但这是一个快速且“肮脏”的解决方案,适用于“标准”组件。
由于代理是为 Internet Explorer 设置的,因此对于每个用户
1) 添加网页浏览器控件
2) 设置控件的属性
姓名:浏览器,可选更改它不可见的大小
3) 将此代码写入工作表,使用浏览器控件
Private Sub browser_DocumentComplete(ByVal pDisp As Object, URL As Variant)
Debug.Print browser.Document.body.innerHTML
End Sub
Private Sub Worksheet_Activate()
browser.Navigate "stackoverflow.com"
End Sub
' Tested on Win7 with Excel 2010
代码有时会做出一些 react (javascript 错误,...),但它使用代理和登录用户。
Update 1:
for other Scenarios, where the Internet Explorer has no proxy set, this could be down (very dirty workaround) by setting the proxy with the registry keys:
HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyServer
Although for this to work, the application would have to terminate all running IE's and then Start the browser.
Disclaimer: I didn't try this (it's just an idea i had), since for my >>needs the above solution is enough, and it seems to me very >>radical/dangerous and there could/should be a better way.
关于excel - 通过具有默认凭据的代理从 Excel 发出 http 获取请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32964337/