WinHttpGetProxyForUrl


M

Malin

Hi,

I'm developing an application which needs to get hold of the client's
proxysettings. In one case they are using a .pac-script.

With the following code, I manage to find the right proxy and everything
works just fine, but when I try to use the auto_detect options, (as can
be seen as comments) it returns error: 12180.

What I want to know is where is it looking for the .pac-script?

Thanks for all the help I can get!! /Malin

[DllImport("winhttp.dll", SetLastError=true, CharSet=CharSet.Unicode)]
private static extern bool WinHttpGetProxyForUrl(
IntPtr http,
string url,
ref WINHTTP_AUTOPROXY_OPTIONS options,
ref WINHTTP_PROXY_INFO info);

[DllImport("winhttp.dll", SetLastError=true, CharSet=CharSet.Unicode)]
private static extern IntPtr WinHttpOpen(
string lpszAgent,
[MarshalAs(UnmanagedType.U4)]
int dwAccessType,
IntPtr lpszProxy,
IntPtr lpszProxyBypass,
[MarshalAs(UnmanagedType.U4)]
int dwFlags);

[DllImport("winhttp.dll", SetLastError=true, CharSet=CharSet.Unicode)]
public static extern bool WinHttpCloseHandle(
IntPtr hInternet);

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

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
private struct WINHTTP_PROXY_INFO
{
[MarshalAs(UnmanagedType.U4)]
private int dwAccessType;
public string lpszProxy;
private string lpszProxyBypass;
}

private static WINHTTP_AUTOPROXY_OPTIONS autoProxyOptions;
private static WINHTTP_PROXY_INFO proxyInfo;
private static IntPtr sessionHandle;
private const int WINHTTP_AUTOPROXY_AUTO_DETECT = 0x00000001;
private const int WINHTTP_AUTO_DETECT_TYPE_DHCP = 0x00000001;
private const int WINHTTP_AUTO_DETECT_TYPE_DNS_A = 0x00000002;
private const int WINHTTP_AUTOPROXY_CONFIG_URL = 0x00000002;
private const int WINHTTP_ACCESS_TYPE_NO_PROXY = 1;
private static IntPtr WINHTTP_NO_PROXY_NAME = IntPtr.Zero;
private static IntPtr WINHTTP_NO_PROXY_BYPASS = IntPtr.Zero;

public static string getDynamicProxy(string url)
{
bool valid = false;
string proxySetting = "";
autoProxyOptions = new WINHTTP_AUTOPROXY_OPTIONS();
proxyInfo = new WINHTTP_PROXY_INFO();
autoProxyOptions.dwFlags =
WINHTTP_AUTOPROXY_CONFIG_URL;//WINHTTP_AUTOPROXY_AUTO_DETECT;
autoProxyOptions.dwAutoDetectFlags = 0;
//(WINHTTP_AUTO_DETECT_TYPE_DHCP|WINHTTP_AUTO_DETECT_TYPE_DNS_A);
autoProxyOptions.lpszAutoConfigUrl = "http://localhost/proxy/proxy.pac";
autoProxyOptions.fAutoLoginIfChallenged = true;
sessionHandle = WinHttpOpen("d&b On-line",
WINHTTP_ACCESS_TYPE_NO_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS,
0);
valid = WinHttpGetProxyForUrl(sessionHandle,url,ref autoProxyOptions,ref
proxyInfo);
System.Console.WriteLine("valid : "+valid+", errorCode:
"+Marshal.GetLastWin32Error());
WinHttpCloseHandle(sessionHandle);
if(valid)
proxySetting = proxyInfo.lpszProxy;
return proxySetting;
}
 
Ad

Advertisements


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