WinHttpGetProxyForUrl

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Malin

    #1

    WinHttpGetProxyForUrl

    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("winh ttp.dll", SetLastError=tr ue, CharSet=CharSet .Unicode)]
    private static extern bool WinHttpGetProxy ForUrl(
    IntPtr http,
    string url,
    ref WINHTTP_AUTOPRO XY_OPTIONS options,
    ref WINHTTP_PROXY_I NFO info);

    [DllImport("winh ttp.dll", SetLastError=tr ue, CharSet=CharSet .Unicode)]
    private static extern IntPtr WinHttpOpen(
    string lpszAgent,
    [MarshalAs(Unman agedType.U4)]
    int dwAccessType,
    IntPtr lpszProxy,
    IntPtr lpszProxyBypass ,
    [MarshalAs(Unman agedType.U4)]
    int dwFlags);

    [DllImport("winh ttp.dll", SetLastError=tr ue, CharSet=CharSet .Unicode)]
    public static extern bool WinHttpCloseHan dle(
    IntPtr hInternet);

    [StructLayout(La youtKind.Sequen tial, CharSet=CharSet .Unicode)]
    private struct WINHTTP_AUTOPRO XY_OPTIONS
    {
    [MarshalAs(Unman agedType.U4)]
    public int dwFlags;
    [MarshalAs(Unman agedType.U4)]
    public int dwAutoDetectFla gs;
    public string lpszAutoConfigU rl;
    private IntPtr lpvReserved;
    [MarshalAs(Unman agedType.U4)]
    private int dwReserved;
    public bool fAutoLoginIfCha llenged;
    }

    [StructLayout(La youtKind.Sequen tial, CharSet=CharSet .Unicode)]
    private struct WINHTTP_PROXY_I NFO
    {
    [MarshalAs(Unman agedType.U4)]
    private int dwAccessType;
    public string lpszProxy;
    private string lpszProxyBypass ;
    }

    private static WINHTTP_AUTOPRO XY_OPTIONS autoProxyOption s;
    private static WINHTTP_PROXY_I NFO proxyInfo;
    private static IntPtr sessionHandle;
    private const int WINHTTP_AUTOPRO XY_AUTO_DETECT = 0x00000001;
    private const int WINHTTP_AUTO_DE TECT_TYPE_DHCP = 0x00000001;
    private const int WINHTTP_AUTO_DE TECT_TYPE_DNS_A = 0x00000002;
    private const int WINHTTP_AUTOPRO XY_CONFIG_URL = 0x00000002;
    private const int WINHTTP_ACCESS_ TYPE_NO_PROXY = 1;
    private static IntPtr WINHTTP_NO_PROX Y_NAME = IntPtr.Zero;
    private static IntPtr WINHTTP_NO_PROX Y_BYPASS = IntPtr.Zero;

    public static string getDynamicProxy (string url)
    {
    bool valid = false;
    string proxySetting = "";
    autoProxyOption s = new WINHTTP_AUTOPRO XY_OPTIONS();
    proxyInfo = new WINHTTP_PROXY_I NFO();
    autoProxyOption s.dwFlags =
    WINHTTP_AUTOPRO XY_CONFIG_URL;//WINHTTP_AUTOPRO XY_AUTO_DETECT;
    autoProxyOption s.dwAutoDetectF lags = 0;
    //(WINHTTP_AUTO_D ETECT_TYPE_DHCP |WINHTTP_AUTO_D ETECT_TYPE_DNS_ A);
    autoProxyOption s.lpszAutoConfi gUrl = "http://localhost/proxy/proxy.pac";
    autoProxyOption s.fAutoLoginIfC hallenged = true;
    sessionHandle = WinHttpOpen("d& b On-line",
    WINHTTP_ACCESS_ TYPE_NO_PROXY,
    WINHTTP_NO_PROX Y_NAME,
    WINHTTP_NO_PROX Y_BYPASS,
    0);
    valid = WinHttpGetProxy ForUrl(sessionH andle,url,ref autoProxyOption s,ref
    proxyInfo);
    System.Console. WriteLine("vali d : "+valid+", errorCode:
    "+Marshal.GetLa stWin32Error()) ;
    WinHttpCloseHan dle(sessionHand le);
    if(valid)
    proxySetting = proxyInfo.lpszP roxy;
    return proxySetting;
    }


    *** Sent via Developersdex http://www.developersdex.com ***
Working...