Proxy settings using WinHTTP and Excel

I am using WinHTTP to execute a GET request in an Excel VBA macro. However, if I try to execute a request from a machine on a network with a proxy server, this will not work. If I configure it manually, it works, but I don’t think that people who will use the tool that I am developing will know their proxies.

Is there a way to auto-configure a proxy server or get a proxy configuration from Windows? Here is 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

In this case, I do not want to force the user to find the address "proxyserver: 8080" - what I want is a way to automatically populate it.

Many thanks.

+3
source share
2 answers

vbScript . , - "proxyserver: 8080":

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

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
+1

- , , , VBA-Web, .

:

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)
+2

All Articles