public class IEProxySetting
{
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) ? : (string.IsNullOrEmpty(exceptions) ? : );
InternetConnectionOption[] options = new InternetConnectionOption[optionCount];
// USE a proxy server ...
options[].m_Option = PerConnOption.INTERNET_PER_CONN_FLAGS;
options[].m_Value.m_Int = (int)((optionCount < ) ? PerConnFlags.PROXY_TYPE_DIRECT : (PerConnFlags.PROXY_TYPE_DIRECT | PerConnFlags.PROXY_TYPE_PROXY));
// use THIS proxy server
if (optionCount > )
{
options[].m_Option = PerConnOption.INTERNET_PER_CONN_PROXY_SERVER;
options[].m_Value.m_StringPtr = Marshal.StringToHGlobalAuto(strProxy);
// except for these addresses ...
if (optionCount > )
{
options[].m_Option = PerConnOption.INTERNET_PER_CONN_PROXY_BYPASS;
options[].m_Value.m_StringPtr = Marshal.StringToHGlobalAuto(exceptions);
}
} // default stuff
list.dwSize = Marshal.SizeOf(list);
list.szConnection = IntPtr.Zero;
list.dwOptionCount = options.Length;
list.dwOptionError = ; 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 = ; 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) ? - : ;
if (returnvalue == )
{ // get the error codes, they might be helpful
returnvalue = Marshal.GetLastWin32Error();
}
// FREE the data ASAP
Marshal.FreeCoTaskMem(optionsPtr);
Marshal.FreeCoTaskMem(ipcoListPtr);
if (returnvalue > )
{ // throw the error codes, they might be helpful
throw new Win32Exception(Marshal.GetLastWin32Error());
} return (returnvalue < );
} /// <summary>
/// 获取正在使用的代理
/// </summary>
/// <returns></returns>
public static string GetProxyServer()
{
//打开注册表
RegistryKey regKey = Registry.CurrentUser;
string SubKeyPath = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
//更改健值,设置代理
RegistryKey optionKey = regKey.OpenSubKey(SubKeyPath, true);
//是否启用 1 启用
int actualProxyStatus = Convert.ToInt32(optionKey.GetValue("ProxyEnable"));
string actualProxy = string.Empty;
if (actualProxyStatus == )
{
actualProxy = optionKey.GetValue("ProxyServer").ToString();
}
regKey.Close();
return actualProxy;
}
} #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()]
public System.Runtime.InteropServices.ComTypes.FILETIME m_FileTime;
[FieldOffset()]
public int m_Int;
[FieldOffset()]
public IntPtr m_StringPtr;
}
}
#endregion #region WinInet enums
//
// options manifests for Internet{Query|Set}Option
//
public enum InternetOption : uint
{
INTERNET_OPTION_PER_CONNECTION_OPTION =
} //
// Options used in INTERNET_PER_CONN_OPTON struct
//
public enum PerConnOption
{
INTERNET_PER_CONN_FLAGS = , // Sets or retrieves the connection type. The Value member will contain one or more of the values from PerConnFlags
INTERNET_PER_CONN_PROXY_SERVER = , // Sets or retrieves a string containing the proxy servers.
INTERNET_PER_CONN_PROXY_BYPASS = , // Sets or retrieves a string containing the URLs that do not use the proxy server.
INTERNET_PER_CONN_AUTOCONFIG_URL = //, // 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);
}

最新文章

  1. php内核分析(二)-ZTS和zend_try
  2. HashMap的内部实现机制,Hash是怎样实现的,什么时候ReHash
  3. 全新的membership框架Asp.net Identity(2)——绕不过的Claims
  4. [HTML/HTML5]7 使用列表
  5. HTML中的属性、段落、标题、换行等
  6. JAVA获取客户端IP地址
  7. 使用XSD校验Mybatis的SqlMapper配置文件(1)
  8. runtime - Method详细笔记
  9. 职责链模式(chain of responsibility Pattern)
  10. C#基础之Attribute
  11. mina 粘包、多包和少包的解决方法
  12. Node.js 4.0.0:灵雀云和 OneAPM 的整合测试
  13. LINUX 系统备份
  14. 浅谈PCB敷铜的“弊与利”
  15. FATE(费用背包,没懂)
  16. #include &lt;fstream&gt;
  17. 连接字符串中Min Pool Size的理解是错误,超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小。
  18. PHP计算上个月的开始时间和结束时间戳
  19. 【响应式编程的思维艺术】 (1)Rxjs专题学习计划
  20. vue构造函数(根实例化时和组件实例对象选项)参数:选项详解

热门文章

  1. 教师信息管理系统(方式一:数据库为oracle数据库;方式二:存储在文件中)
  2. Git忽略提交规则 - .gitignore配置运维总结
  3. RoR - Introduction to Active Record
  4. git 远程删除文件
  5. POSTGRESQL中ERROR: recursive query &quot;t&quot; column 2 has type character varying(150) in non-recursive term but type character varying overall
  6. jQuery学习--Code Organization Concepts
  7. iot-web增加apis-namespace组件
  8. asp.net机制理解(Javaweb同理)
  9. 台式电脑、笔记本快捷选择启动项Boot 快捷键大全
  10. ASM磁盘组剔盘、加盘实施过程