C#webBrowser使用代理服务器的方法winform
2015-07-09 00:00
627 查看
http://www.sufeinet.com/thread-2242-1-1.html
其实在C#中使用webBrowser大家应该都会了,论坛也有很多相前的例子大家可以查询一下就知道了
但是像直接使用浏览器一样设置代理 的方法可能很多人还不知道吧。
这个其实是调用一个Dll文件进行设置的,
下面大家跟我一起来看看吧
首先还是要先建一个结构就是代理信息的结构体
如下
///
/// 代理结构体
///
public struct Struct_INTERNET_PROXY_INFO
{
public int dwAccessType;
public IntPtr proxy;//IP以及端口号
public IntPtr proxyBypass;
};
下面是如何 设置代理 的具体实现
///
/// 设置代理的Api
///
///
[DllImport(“wininet.dll”, SetLastError = true)]
private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);
使用的时候也非常的简单
RefreshIESettings(“41.129.53.227:80”);
webBrowser1.Navigate(“http://www.sufeinet.com“);
这样就可以了。
其实在C#中使用webBrowser大家应该都会了,论坛也有很多相前的例子大家可以查询一下就知道了
但是像直接使用浏览器一样设置代理 的方法可能很多人还不知道吧。
这个其实是调用一个Dll文件进行设置的,
下面大家跟我一起来看看吧
首先还是要先建一个结构就是代理信息的结构体
如下
///
/// 代理结构体
///
public struct Struct_INTERNET_PROXY_INFO
{
public int dwAccessType;
public IntPtr proxy;//IP以及端口号
public IntPtr proxyBypass;
};
下面是如何 设置代理 的具体实现
///
/// 设置代理的Api
///
///
[DllImport(“wininet.dll”, SetLastError = true)]
private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);
/// <summary> /// 代理IP以及端口号 /// </summary> /// <param name="strProxy"></param> private void RefreshIESettings(string strProxy) { const int INTERNET_OPTION_PROXY = 38; const int INTERNET_OPEN_TYPE_PROXY = 3; 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)); // Converting structure to IntPtr Marshal.StructureToPtr(struct_IPI, intptrStruct, true); bool iReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY, intptrStruct, Marshal.SizeOf(struct_IPI)); }[
使用的时候也非常的简单
RefreshIESettings(“41.129.53.227:80”);
webBrowser1.Navigate(“http://www.sufeinet.com“);
这样就可以了。
相关文章推荐
- C#操作剪切板内容
- c# webconfig配置修改
- 用C#来查看电脑硬件和系统信息
- C# 课堂总结4-类(常用的类)
- Csharp学习第三天
- C#利用最新版的WPS实现导入导出
- C# COM组件操作Excel
- C# COM组件操作Excel
- C#接口的理解
- C#运算符的优先级
- [2]项目创建-使用C#.NET开发基于本地数据缓存的PC客户端
- 初次接触C#
- C#定时器
- [1]开发准备-使用C#.NET开发基于本地数据缓存的PC客户端
- c#定时器和global实现自动job示例
- c#第五次作业
- C#操作Excel
- 比较好的C#编程个人博客地址
- 【C#】【Thread】ManualResetEvent和AutoResetEvent区别
- (收藏)C#实现截屏