0

I would like to use a proxy in my webbrowser but without editing the registry on the machine.

4

1 回答 1

3

You could use WMI, but that would still adjust the system settings. If you want to adjust only the proxy settings for your own process, you can do that via UrlMkSetSessionOption exposed by urlmon.dll. An example of this function is listed below. For more information on INTERNET_OPTION_PROXY, see http://msdn.microsoft.com/en-us/library/windows/desktop/aa385328(v=vs.85).aspx.

private void SetSessionProxy(strin ProxyAddress, string BypassList)
{
    var proxyInfo= new INTERNET_PROXY_INFO {
        dwAccessType = 0x3,
        lpszProxy = ProxyAddress,
        lpszProxyBypass = BypassList
    };
    int structSize = Marshal.SizeOf(proxyInfo);
    const uint SetProxy = 0x26;

    if (Win32Native.UrlMkSetSessionOption(SetProxy, structure, dwLen, 0) != 0)
        throw new Win32Exception();
}

[StructLayout(LayoutKind.Sequential)]
private class INTERNET_PROXY_INFO
{
    public uint dwAccessType;
    [MarshalAs(UnmanagedType.LPStr)]
    public string lpszProxy;
    [MarshalAs(UnmanagedType.LPStr)]
    public string lpszProxyBypass;
}

[DllImport("urlmon.dll", CharSet=CharSet.Unicode, SetLastError=true)]
private static extern int UrlMkSetSessionOption(uint dwOption, INTERNET_PROXY_INFO structNewProxy, uint dwLen, uint dwZero);
于 2012-10-06T20:44:41.467 回答