webBrowser代理设置c#代码

 更新时间:2012年11月30日 10:36:06   作者:  
本文将介绍C# 为webBrowser设置代理实现代码,需要了解的朋友可以参考下
为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);
}
}
*/

相关文章

  • 基于WPF平台使用纯C#实现动态处理json字符串

    基于WPF平台使用纯C#实现动态处理json字符串

    在当今的软件开发领域,数据的交换与存储变得愈发频繁,JSON作为一种轻量级的数据交换格式,在 WPF平台开发的桌面应用里,我们常常需要与各种数据源交互,动态处理JSON字符串就成为了一项必备技能,本文将深入探讨如何在 WPF 平台上,仅使用纯C#代码实现对JSON字符串的动态处理
    2025-01-01
  • C#开发教程之ftp操作方法整理

    C#开发教程之ftp操作方法整理

    这篇文章主要介绍了C#开发教程之ftp操作方法整理的相关资料,需要的朋友可以参考下
    2016-07-07
  • C#使用AnimateWindow()实现动画窗体的方法

    C#使用AnimateWindow()实现动画窗体的方法

    用API函数AnimateWindow函数来实现窗体的动画效果,在C#中,你可以使用P/Invoke技术调用Windows API中的AnimateWindow函数来实现动画窗体,本文就给大家介绍了C#使用AnimateWindow()实现动画窗体的方法,感兴趣的朋友可以参考下
    2024-04-04
  • C#调用HTTP POST请求上传图片的示例代码

    C#调用HTTP POST请求上传图片的示例代码

    现在很多B/S系统的开发都是通过API方式来进行的,一般服务端会开放一个API接口,客户端调用API接口来实现图片或文件上传的功能,感兴趣的可以了解一下
    2021-05-05
  • C#制作简易的屏保

    C#制作简易的屏保

    这篇文章主要为大家详细介绍了C#制作简易的屏保的相关资料,C#如何制作屏保的过程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-01-01
  • c#判断网络连接状态

    c#判断网络连接状态

    有时我们要不停的判断网络的连接状态,比如服务器网络连接是否正常等,下面就简单介绍我的判断方法
    2014-01-01
  • 基于DateTime.ParseExact方法的使用详解

    基于DateTime.ParseExact方法的使用详解

    本篇文章是对DateTime.ParseExact方法的使用进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • 对C#中public、private、protect的区别说明

    对C#中public、private、protect的区别说明

    这篇文章主要介绍了对C#中public、private、protect的区别说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-04-04
  • 基于C#实现的轻量级多线程队列图文详解

    基于C#实现的轻量级多线程队列图文详解

    这篇文章主要给大家介绍了关于基于C#实现的轻量级多线程队列的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用C#具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-08-08
  • C#创建、读取和修改Excel的方法

    C#创建、读取和修改Excel的方法

    这篇文章主要介绍了C#创建、读取和修改Excel的方法,涉及C#使用Jet OLE DB操作Excel的技巧,非常具有实用价值,需要的朋友可以参考下
    2015-04-04

最新评论