excel - 通过具有默认凭据的代理从 Excel 发出 http 获取请求

标签 excel vba proxy

我想从一个网站(只是一个简单的 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) 添加网页浏览器控件

enter image description here
enter image description here

2) 设置控件的属性
姓名:浏览器,可选更改它不可见的大小

enter image description here

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/

相关文章:

c++ - 如何使用C++读取PAC文件

java - 我可以代理嵌入在 WAR 中的 Golang 服务器吗?

excel - 如何保持所有工作表中的页眉(非静态页眉)相同?

arrays - vba,选择一个动态矩阵

python - 添加行/列时维护公式

vba - Excel Vba 单元格的异常行为。在合并的单元格上查找

vba - Excel VBA循环遍历所有工作簿和所有工作表

excel - 发送 key 和应用程序激活

vba - 希望在 Excel 中选择不确定数量的行作为较大 VBA 宏的一部分

Azure 应用程序代理 COR