使用WinHTTP和Excel配置代理 [英] Configuring proxy with WinHTTP and Excel

查看:810
本文介绍了使用WinHTTP和Excel配置代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用WinHTTP在Excel VBA宏中执行GET请求。但是,如果我尝试从具有代理的网络中的计算机执行请求,则不起作用。如果我手动配置它,它是有效的,但我不认为使用我正在开发的工具的人会知道他们的代理服务器。

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.

是否一种自动配置代理服务器的方法,或从Windows获取代理配置?以下是一个示例代码:

Is there a way to autoconfigure the proxy server, or to get the proxy configuration from Windows? Here follows a sample code:

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

在这种情况下,我不想强制用户找到代理服务器:8080地址 - 我想要的是一种自动填充的方式。

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.

非常感谢。 $ b

推荐答案

从下面的链接我得到了下面的vbScript。您可能可以使用代理服务器,并将其作为变量传递给代理服务器proxyserver:8080:

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":

http://www.activexperts.com/activmonitor/windowsmanagement/scripts/networking/client/retrieving/

如果你知道vbScript - 这与VBA非常相似,我认为这应该有很多帮助。如果您需要帮助,请在VBA中写下这个内容,让我知道。

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.

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

这篇关于使用WinHTTP和Excel配置代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆