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);
}

C#设置IE代理的更多相关文章

  1. C#设置IE代理及遇到问题的解决方案

    起初使用的方法是修改完一次代理之后就不能继续修改,需要重新启动一次进程才可以,最初代码是: private void ShowProxyInfo() { if (!GetProxyStatus()) ...

  2. C#设置通过代理访问ftp服务器

    // 创建FTP连接 private FtpWebRequest CreateFtpWebRequest(string uri, string requestMethod) { FtpWebReque ...

  3. atitit agt sys 设置下级代理功能设计.docx

    atitit agt sys 设置下级代理功能设计.docx 显示界面1 先查询显示 set_sub.js1 设置代理2 /atiplat_cms/src/com/attilax/user/Agent ...

  4. Nginx_地址重写(rewrite)_日志管理(log_format)_压缩输出_Nginx设定限速_Nginx设置反向代理及反向代理缓存

    Nginx地址重写 Nginx rewrite rewrite语法规则1).变量名可以使用 "=" 或 "!=" 运算符~ 区分大小写~* 不区分大小写^~ 禁 ...

  5. maven3实战之设置HTTP代理

    maven3实战之设置HTTP代理 ---------- 有时候你所在的公司基于安全因素考虑,要求你使用通过安全认证的代理访问因特网.这种情况下,就需要为Maven配置HTTP代理,才能让它正常访问外 ...

  6. 快捷设置IE代理小工具

    时间:2015-02-06 起因: 公司新装了PLM系统,用这个系统必须使用指定IP段的IP才能访问.所以为了还能愉快的继续使用代理进行特定网站的访问,我们必须要频繁的去设置IE代理,这也太麻烦了吧. ...

  7. 设置HTTP代理

    Maven通过<<UserHome>>/.m2/settings.xml(如果没有该文件,复制<<MavenHome>>/conf/settings.x ...

  8. 在cocos2d-x jsb/html5中设置触摸代理的方法

    和官方的说明不同,js binding的很多api和ch5版是不一样的.遇到不一样的就需要我们努力去看源码寻找了. 主要是以下几个文件 cocos2d_specifics.cpp cocos2d_sp ...

  9. 转:设置HtmlUnitDriver代理及处理用户验证有关问题

    selenium2 提供了一种无ui模式的driver,即htmlunitdriver.特点运行比较快.其实htmlunitdriver 是对htmlunit 的封装,这样大家就可以使用自己习惯sel ...

  10. 主机设置ss代理,虚拟机共享代理

    代理的原理: 关于代理的具体的书面定义你百度谷歌可以知道.这里,我想简单通过一个例子,说明代理的原理: 假如,你在北京,但你女朋友在广州,你有东西要给你的女朋友,但是正好你这几天公司有事,所以你不能去 ...

随机推荐

  1. docker 打卡

    create 2019/01/01 mod 2019/02/02 安装没得技术含量,看过菜鸟教程和纯洁写的博客,感觉so easy 命令: yum install docker 启动 设置开机启动 s ...

  2. ubuntu安装mysql-connector-python

    在安装MySQL-python时遇到报错: sudo pip install MySQL-python Traceback (most recent call last): File "&l ...

  3. PHP(javascript基础)

    js浏览器的脚本语言js的基础语法和 . js Dom操作写法分类1.行内(内联)写在标签里面,以属性的形式表现,属性名是“事件属性名” 例如:<button onClick="js代 ...

  4. Python学习之旅(二十)

    Python基础知识(19):面向对象高级编程(Ⅱ) 定制类 形如“__xx__”的变量或函数在Python中是有特殊用途的 1.__str__ 让打印出来的结果更好看 __str__:面向用户:__ ...

  5. vivado/FPGA 使用小纪

    1.使用FPGA做为外部控制器的总线译码时,将总线时钟接在全局时钟脚上(MRCC),就算接在了局部时钟(SRCC)上,也要通过BUFG转为全局时钟走线,否则会因为local clk到各部分的时延较大引 ...

  6. 图像分类(三)GoogLenet Inception_v3:Rethinking the Inception Architecture for Computer Vision

    Inception V3网络(注意,不是module了,而是network,包含多种Inception modules)主要是在V2基础上进行的改进,特点如下: 将滤波器尺寸(Filter Size) ...

  7. 19.1-uC/OS-III内存管理应用

    一个处理器,在不断地分配和释放内存的过程中,一整块连续的内存被分散为很多离散的小块内存, 这些叫做内存碎片, 内存碎片过多会导致内存的浪费. uC/OS 的内存管理机制就是为了尽量减少内存碎片.大致的 ...

  8. javaweb(4)之Listener&Filter

    监听器 (Listener) 介绍 监听器用于监听 web 应用中某些对象.信息的创建.销毁.增加,修改,删除等动作的发生,然后作出相应的响应处理.当范围对象的状态发生变化的时候,服务器自动调用监听器 ...

  9. Linux的is not in the sudoers file 解决

    https://blog.csdn.net/hxpjava1/article/details/79566822

  10. mongoose findByIdAndUpdate不执行的解决方法

    请参考Mongoose的文档 1.findOneAndUpdate([query], [doc], [options], [callback]) 有callback传递才执行. 2.exec是prom ...