【问题标题】:Setting IE Proxy by C#通过 C# 设置 IE 代理
【发布时间】:2011-07-10 03:14:15
【问题描述】:

您好,我想使用 C# 程序设置 IE 代理,因为 WebProxy 类有获取代理方法。但是没有设置它的方法!

【问题讨论】:

    标签: c# proxy ip


    【解决方案1】:

    以下是通过谷歌搜索找到的一些替代方案:

    1- 全局代理选择

    这是来自http://www.hccp.org/csharp-http-proxy.html

    示例代码:

    System.Net.Uri proxyURI = new System.Net.Uri("http://64.202.165.130:3128");
    System.Net.GlobalProxySelection.Select = new System.Net.WebProxy(proxyURI);
    

    2- StackOverflow 上的另一个讨论:Programmatically Set Browser Proxy Settings in C#

    如果您只想为您的应用使用代理,请阅读它。

    对于全球变化,建议查看:http://msdn.microsoft.com/en-us/library/aa384113.aspx

    示例代码:

    WINHTTP_PROXY_INFO proxyInfo;
    
    // Allocate memory for string members.
    proxyInfo.lpszProxy = new WCHAR[25];
    proxyInfo.lpszProxyBypass = new WCHAR[25];
    
    // Set the members of the proxy info structure.
    proxyInfo.dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
    swprintf_s(proxyInfo.lpszProxy, 25, L"proxy_server");
    swprintf_s(proxyInfo.lpszProxyBypass, 25, L"<local>");
    
    // Set the default proxy configuration.
    if (WinHttpSetDefaultProxyConfiguration( &proxyInfo ))
        printf("Proxy Configuration Set.\n");
    
    // Free memory allocated to the strings.
    delete [] proxyInfo.lpszProxy;
    delete [] proxyInfo.lpszProxyBypass;
    

    3- 使用本机代码

    这是来自http://huddledmasses.org/setting-windows-internet-connection-proxy-from-c/

    示例代码:

    using System;
    using System.Runtime.InteropServices;
    using System.ComponentModel;
    
    namespace PoshHttp
    {
       public class Proxies
       {
          public static bool UnsetProxy()
          {
             return SetProxy(null, null);
          }
          public static bool SetProxy(string strProxy)
          {
             return SetProxy(strProxy, null);
          }
    
          public static bool SetProxy(string strProxy, string exceptions)
          {
             InternetPerConnOptionList list = new InternetPerConnOptionList();
    
             int optionCount = string.IsNullOrEmpty(strProxy) ? 1 : (string.IsNullOrEmpty(exceptions) ? 2 : 3);
             InternetConnectionOption[] options = new InternetConnectionOption[optionCount];
             // USE a proxy server ...
             options[0].m_Option = PerConnOption.INTERNET_PER_CONN_FLAGS;
             options[0].m_Value.m_Int = (int)((optionCount < 2) ? PerConnFlags.PROXY_TYPE_DIRECT : (PerConnFlags.PROXY_TYPE_DIRECT | PerConnFlags.PROXY_TYPE_PROXY));
             // use THIS proxy server
             if (optionCount > 1)
             {
                options[1].m_Option = PerConnOption.INTERNET_PER_CONN_PROXY_SERVER;
                options[1].m_Value.m_StringPtr = Marshal.StringToHGlobalAuto(strProxy);
                // except for these addresses ...
                if (optionCount > 2)
                {
                   options[2].m_Option = PerConnOption.INTERNET_PER_CONN_PROXY_BYPASS;
                   options[2].m_Value.m_StringPtr = Marshal.StringToHGlobalAuto(exceptions);
                }
             }
    
             // default stuff
             list.dwSize = Marshal.SizeOf(list);
             list.szConnection = IntPtr.Zero;
             list.dwOptionCount = options.Length;
             list.dwOptionError = 0;
    
    
             int optSize = Marshal.SizeOf(typeof(InternetConnectionOption));
             // make a pointer out of all that ...
             IntPtr optionsPtr = Marshal.AllocCoTaskMem(optSize * options.Length);
             // copy the array over into that spot in memory ...
             for (int i = 0; i < options.Length; ++i)
             {
                IntPtr opt = new IntPtr(optionsPtr.ToInt32() + (i * optSize));
                Marshal.StructureToPtr(options[i], opt, false);
             }
    
             list.options = optionsPtr;
    
             // and then make a pointer out of the whole list
             IntPtr ipcoListPtr = Marshal.AllocCoTaskMem((Int32)list.dwSize);
             Marshal.StructureToPtr(list, ipcoListPtr, false);
    
             // and finally, call the API method!
             int returnvalue = NativeMethods.InternetSetOption(IntPtr.Zero,
                InternetOption.INTERNET_OPTION_PER_CONNECTION_OPTION, 
                ipcoListPtr, list.dwSize) ? -1 : 0;
             if (returnvalue == 0)
             {  // get the error codes, they might be helpful
                returnvalue = Marshal.GetLastWin32Error();
             }
             // FREE the data ASAP
             Marshal.FreeCoTaskMem(optionsPtr);
             Marshal.FreeCoTaskMem(ipcoListPtr);
             if (returnvalue > 0)
             {  // throw the error codes, they might be helpful
                throw new Win32Exception(Marshal.GetLastWin32Error());
             }
    
             return (returnvalue < 0);
          }
       }
    
       #region WinInet structures
       [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
       public struct InternetPerConnOptionList
       {
          public int dwSize;               // size of the INTERNET_PER_CONN_OPTION_LIST struct
          public IntPtr szConnection;         // connection name to set/query options
          public int dwOptionCount;        // number of options to set/query
          public int dwOptionError;           // on error, which option failed
          //[MarshalAs(UnmanagedType.)]
          public IntPtr options;
       };
    
       [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
       public struct InternetConnectionOption
       {
          static readonly int Size;
          public PerConnOption m_Option;
          public InternetConnectionOptionValue m_Value;
          static InternetConnectionOption()
          {
             InternetConnectionOption.Size = Marshal.SizeOf(typeof(InternetConnectionOption));
          }
    
          // Nested Types
          [StructLayout(LayoutKind.Explicit)]
          public struct InternetConnectionOptionValue
          {
             // Fields
             [FieldOffset(0)]
             public System.Runtime.InteropServices.ComTypes.FILETIME m_FileTime;
             [FieldOffset(0)]
             public int m_Int;
             [FieldOffset(0)]
             public IntPtr m_StringPtr;
          }
       }
       #endregion
    
       #region WinInet enums
       //
       // options manifests for Internet{Query|Set}Option
       //
       public enum InternetOption : uint
       {
          INTERNET_OPTION_PER_CONNECTION_OPTION = 75
       }
    
       //
       // Options used in INTERNET_PER_CONN_OPTON struct
       //
       public enum PerConnOption
       {
          INTERNET_PER_CONN_FLAGS = 1, // Sets or retrieves the connection type. The Value member will contain one or more of the values from PerConnFlags 
          INTERNET_PER_CONN_PROXY_SERVER = 2, // Sets or retrieves a string containing the proxy servers.  
          INTERNET_PER_CONN_PROXY_BYPASS = 3, // Sets or retrieves a string containing the URLs that do not use the proxy server.  
          INTERNET_PER_CONN_AUTOCONFIG_URL = 4//, // Sets or retrieves a string containing the URL to the automatic configuration script.  
    
       }
    
       //
       // PER_CONN_FLAGS
       //
       [Flags]
       public enum PerConnFlags
       {
          PROXY_TYPE_DIRECT = 0x00000001,  // direct to net
          PROXY_TYPE_PROXY = 0x00000002,  // via named proxy
          PROXY_TYPE_AUTO_PROXY_URL = 0x00000004,  // autoproxy URL
          PROXY_TYPE_AUTO_DETECT = 0x00000008   // use autoproxy detection
       }
       #endregion
    
       internal static class NativeMethods
       {
          [DllImport("WinInet.dll", SetLastError = true, CharSet = CharSet.Auto)]
          [return: MarshalAs(UnmanagedType.Bool)]
          public static extern bool InternetSetOption(IntPtr hInternet, InternetOption dwOption, IntPtr lpBuffer, int dwBufferLength);
       }
    }
    

    请查看链接本身以获取详细信息和解决方案的完整部分。

    【讨论】:

    • 谢谢,我使用解决方案2,但不要在windows7中为我设置代理。我想在win7中为所有浏览器设置代理,我可以使用解决方案3吗?
    • 解决方案 2 适用于特定于应用程序的代理,而不是全局代理。尝试解决方案 1。请注意,Chrome 使用 IE 代理,而 Firefox 可以选择使用 IE 代理(称为“系统”代理)或设置自己的。
    猜你喜欢
    • 1970-01-01
    • 2016-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多