博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 为webBrowser设置代理
阅读量:7064 次
发布时间:2019-06-28

本文共 2787 字,大约阅读时间需要 9 分钟。

http://blog.163.com/weipeng_yyp/blog/static/122478198201132223442853/

 

为webBrowser设置代理:

public struct Struct_INTERNET_PROXY_INFO

{

public int dwAccessType;

public IntPtr proxy;

public IntPtr proxyBypass;

};

[DllImport("wininet.dll", SetLastError = true)]

private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);

 

private void RefreshIESettings(string strProxy)//strProxy为代理IP:端口

{

const int INTERNET_OPTION_PROXY = 38;

const int INTERNET_OPEN_TYPE_PROXY = 3;

const int INTERNET_OPEN_TYPE_DIRECT = 1;

 

Struct_INTERNET_PROXY_INFO struct_IPI;

 

 

 

// Filling in structure

struct_IPI.dwAccessType = INTERNET_OPEN_TYPE_PROXY;

struct_IPI.proxy = Marshal.StringToHGlobalAnsi(strProxy);

struct_IPI.proxyBypass = Marshal.StringToHGlobalAnsi("local");

 

// Allocating memory

IntPtr intptrStruct = Marshal.AllocCoTaskMem(Marshal.SizeOf(struct_IPI));

if (string.IsNullOrEmpty(strProxy) || strProxy.Trim().Length == 0)

{

strProxy = string.Empty;

struct_IPI.dwAccessType = INTERNET_OPEN_TYPE_DIRECT;

 

}

// Converting structure to IntPtr

Marshal.StructureToPtr(struct_IPI, intptrStruct, true);

 

bool iReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY, intptrStruct, Marshal.SizeOf(struct_IPI));

}

使用:RefreshIESettings("xxx.xxx.xxx.xxx:xx");

 

--------------------------------------------------------------------------------

完美方法:

/*完整解析

public class IEProxy

{

private const int INTERNET_OPTION_PROXY = 38;

private const int INTERNET_OPEN_TYPE_PROXY = 3;

private const int INTERNET_OPEN_TYPE_DIRECT = 1;

 

private string ProxyStr;

 

 

[DllImport("wininet.dll", SetLastError = true)]

 

private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);

 

public struct Struct_INTERNET_PROXY_INFO

{

public int dwAccessType;

public IntPtr proxy;

public IntPtr proxyBypass;

}

 

private bool InternetSetOption(string strProxy)

{

int bufferLength;

IntPtr intptrStruct;

Struct_INTERNET_PROXY_INFO struct_IPI;

 

if (string.IsNullOrEmpty(strProxy) || strProxy.Trim().Length == 0)

{

strProxy = string.Empty;

struct_IPI.dwAccessType = INTERNET_OPEN_TYPE_DIRECT;

 

}

else

{

struct_IPI.dwAccessType = INTERNET_OPEN_TYPE_PROXY;

}

struct_IPI.proxy = Marshal.StringToHGlobalAnsi(strProxy);

struct_IPI.proxyBypass = Marshal.StringToHGlobalAnsi("local");

bufferLength = Marshal.SizeOf(struct_IPI);

intptrStruct = Marshal.AllocCoTaskMem(bufferLength);

Marshal.StructureToPtr(struct_IPI, intptrStruct, true);

return InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY, intptrStruct, bufferLength);

 

}

public IEProxy(string strProxy)

{

this.ProxyStr = strProxy;

}

//设置代理

public bool RefreshIESettings()

{

return InternetSetOption(this.ProxyStr);

}

//取消代理

public bool DisableIEProxy()

{

return InternetSetOption(string.Empty);

}

}

 

*/

转载地址:http://yxnll.baihongyu.com/

你可能感兴趣的文章
素材锦囊——50个高质量的 PSD 素材免费下载《上篇》
查看>>
【转】oc中消息传递机制-附:对performSelector方法的扩充
查看>>
oracle的nvl和sql server的isnull
查看>>
[转]虚拟机下Ubuntu共享主机文件(Ubuntu、VMware、共享)
查看>>
高血压 治疗 偏方
查看>>
HtmlAttribute HTML属性处理类
查看>>
[书目20130316]jQuery UI开发指南
查看>>
Sql Server系列:开发存储过程
查看>>
Find INTCOL#=1001 in col_usage$?
查看>>
AutoCAD 命令统计魔幻球的实现过程--(3)
查看>>
dp学习笔记1
查看>>
newlisp debugger
查看>>
Java进阶02 异常处理
查看>>
图文介绍openLDAP在windows上的安装配置
查看>>
Heritrix 3.1.0 源码解析(十二)
查看>>
java 动态代理
查看>>
微信5.0绑定银行卡教程
查看>>
数字转换为壹仟贰佰叁拾肆的Java方法
查看>>
引发网页布局灾难的7个大错误
查看>>
一个表单对应多个提交按钮,每个提交按钮对应不同的行为
查看>>