vba 使用 WinHTTP 和 Excel 配置代理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10590219/
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
Configuring proxy with WinHTTP and Excel
提问by Yuri Oliveira
I'm using WinHTTP to do a GET request in an Excel VBA Macro. However, if I try to do the request from a machine in a network with a proxy, it does not work. If I configure it manually, it works, but I don't think the people who'll use the tool I'm developing will know their proxy servers.
我正在使用 WinHTTP 在 Excel VBA 宏中执行 GET 请求。但是,如果我尝试从具有代理的网络中的机器发出请求,则它不起作用。如果我手动配置它,它会起作用,但我认为使用我正在开发的工具的人不会知道他们的代理服务器。
Is there a way to autoconfigure the proxy server, or to get the proxy configuration from Windows? Here follows a sample code:
有没有办法自动配置代理服务器,或者从 Windows 获取代理配置?下面是一个示例代码:
Dim result As String
Dim URL As String
Dim winHttpReq As Object
Set winHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")
URL = "http://google.com/"
winHttpReq.Open "GET", URL, False
winHttpReq.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
winHttpReq.setProxy 2, "proxyserver:8080", ""
winHttpReq.send
result = winHttpReq.responseText
In this case, I don't want to force the user to find the "proxyserver:8080" address - what I want is a way to fill that automatically.
在这种情况下,我不想强迫用户找到“proxyserver:8080”地址——我想要的是一种自动填充的方法。
Thanks a lot.
非常感谢。
采纳答案by Scott Holtzman
I got the below vbScript from the following link. You may be able to use to get the proxy server and pass it as a variable to your code for "proxyserver:8080":
我从以下链接获得了以下 vbScript。您可以使用 获取代理服务器并将其作为变量传递给“proxyserver:8080”的代码:
http://www.activexperts.com/activmonitor/windowsmanagement/scripts/networking/client/retrieving/
http://www.activexperts.com/activmonitor/windowsmanagement/scripts/networking/client/retrieving/
If you know vbScript - which is very similar to VBA, I think this should help a lot. If you need help writing this in VBA, let me know.
如果您知道 vbScript - 它与 VBA 非常相似,我认为这应该会有很大帮助。如果您需要帮助在 VBA 中编写此文件,请告诉我。
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Proxy")
For Each objItem in colItems
Wscript.Echo "Proxy Port Number: " & objItem.ProxyPortNumber
Wscript.Echo "Proxy Server: " & objItem.ProxyServer
Wscript.Echo "Server Name: " & objItem.ServerName
Wscript.Echo
Next
回答by Alexandre D'Erman
In case anyone else stumbles on this page looking for an answer to this same question, I'd like to point to this answer, which mentions using the VBA-Webproject to solve this exact problem.
如果其他人偶然在此页面上寻找同一问题的答案,我想指出这个答案,其中提到使用VBA-Web项目来解决这个确切的问题。
Your code would then look something like this:
您的代码将如下所示:
Dim client As New WebClient
With client
.BaseUrl = "https://www.google.com"
.EnableAutoProxy = True
End With
Dim request As New WebRequest
With request
.Method = WebMethod.HttpGet
.AddHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
.Format = WebFormat.PlainText
End With
Dim response As WebResponse
Set response = client.Execute(request)