using proxy auto config (.PAC) script for web request

G

Guest

I am having trouble accessing a web page programmatically from .NET. Our
proxy server is determined by a PAC script and all the standard .NET (and
WinHttp) proxy techniques don't work for scripts.

My current state is that I use the WinInet function
'InternetQueryOptionList' to get the proxy settings. This is working fine, I
am able to detect that my IE settings use a PAC script.

Next, I try using the WinHttp function 'WinHttpDetectAutoProxyConfigUrl' to
get the proxy address. This always fails. If I pass 0 for the handle, it
returns error 6 (invalid handle). If I use WinHttpOpen to get a handle and
pass that into the function, it returns error 87 (invalid parms).

I'm guessing that my marshalling is incorrect, so I include the code below.
Any help is appreciated.

Thanks, Richard

public class WinHttpWrapper
{
[System.Runtime.InteropServices.DllImport("winhttp.dll", EntryPoint =
"WinHttpGetProxyForUrl")]
public extern static bool WinHttpGetProxyForUrl(IntPtr handle, string url,
ref WINHTTP_AUTOPROXY_OPTIONS proxyOptions, out WINHTTP_PROXY_INFO proxyInfo);

[System.Runtime.InteropServices.DllImport("winhttp.dll", EntryPoint =
"WinHttpDetectAutoProxyConfigUrl")]
public extern static bool WinHttpDetectAutoProxyConfigUrl(uint
autoDetectFlags, out string url);

[System.Runtime.InteropServices.DllImport("winhttp.dll", EntryPoint =
"WinHttpOpen")]
public extern static IntPtr WinHttpOpen(string userAgent, uint accessType,
string proxyName, string bypassName, uint flags);

[System.Runtime.InteropServices.DllImport("winhttp.dll", EntryPoint =
"WinHttpCloseHandle")]
public extern static bool WinHttpCloseHandle(IntPtr handle);
}

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
public struct WINHTTP_AUTOPROXY_OPTIONS
{
public uint dwFlags;
public uint dwAutoDetectFlags;
public string lpszAutoConfigUrl;
public IntPtr lpvReserved;
public uint dwReserved;
public bool fAutoLoginIfChallenged;
}

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
public struct WINHTTP_PROXY_INFO
{
public uint dwAccessType;
public string lpszProxy;
public string lpszProxyBypass;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top