C# WebBrowser设置代理
WebBrowser控件是基于IE浏览器的,所以它的内核功能是依赖于IE. code:
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; //You can change the proxy with InternetSetOption method from the wininet.dll
//这个就是设置一个Internet 选项。设置代理是Internet 选项其中的一个功能
[System.Runtime.InteropServices.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;
}
//设置代理的方法
//strProxy为代理IP:端口
private bool InternetSetOption(string strProxy)
{
int bufferLength;
IntPtr intptrStruct;
Struct_INTERNET_PROXY_INFO struct_IPI; //Filling in structure
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); //Allocating memory
//关联到内存
intptrStruct = Marshal.AllocCoTaskMem(bufferLength); //Converting structure to IntPtr
//把结构体转换到句柄
Marshal.StructureToPtr(struct_IPI, intptrStruct, true);
return InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY, intptrStruct, bufferLength);
}
public IEProxy(string strProxy)
{
this.ProxyStr = strProxy;
}
//设置代理
public bool SetIESettings()
{
return InternetSetOption(this.ProxyStr);
}
//取消代理
public bool DisableIEProxy()
{
return InternetSetOption(string.Empty);
}
}
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
C# WebBrowser设置代理的更多相关文章
- C# 为WebBrowser设置代理,打开网页
WebBrowser控件是基于IE浏览器的,所以它的内核功能是依赖于IE的,相信做.NET的人都知道. 今天的主题,和上一篇文章应该是差不多的,都是通过代理来实现功能的. 请看下面的代码: //1.定 ...
- C# WebBrowser 设置代理完全解决方案
微软webbrowser控件也就是IE插件,他的所有功能就像IE类似,当然设置也是一样的,下面介绍下webbrowser如何设置代理,可不要用这个对抗广告联盟哦 You can change the ...
- SSH免手动输入密码和设置代理
通过使用sshpass将密码写入命令里,直接执行,免去手动密码输入的步骤命令如下: sshpass -p password_abc ssh user_abc@ssh_host -p ssh_port ...
- 如何在android studio 1.0 启动时设置代理【解决WARN - ateSettings.impl.UpdateChecker - Connection failed.】
今天第一次用android studio,下了个比较新的1.0.1 linux版本,结果启动时老是出现以下错误: [ 6987] WARN - ateSettings.impl.UpdateCheck ...
- Mac SVN 设置代理(Proxy)并 Checkout 代码
1. 设置代理 Finder 菜单里面 -> 前往 -> 前往文件夹 -> /Users/username/.subversion -> 编辑文件 servers 在文件的 [ ...
- java中设置代理的两种方式
1 前言 有时候我们的程序中要提供可以使用代理访问网络,代理的方式包括http.https.ftp.socks代理.比如在IE浏览器设置代理. 那我们在我们的java程序中使用代理呢,有如下两种方式. ...
- cxf设置代理访问webservice接口
由于业务上的需要,需要访问第三方提供的webservice接口,但由于公司做了对外访问的限制,不设置代理是不能外网的,如果使用http设置代理访问外网还是比较容易的,但使用cxf有点不知道从哪里入手. ...
- 虚拟机 centos设置代理上网
假设我们要设置代理为 IP:PORT 1.网页上网 网页上网设置代理很简单,在firefox浏览器下 Edit-->>Preferences-->>Advanced--> ...
- 为Mac Terminal设置代理
参考链接:http://tz101.github.io/new-mac-os-x-yosemite-10-10-Xcode-Brew-Shadowsocks-Proxychains/ https:// ...
随机推荐
- radom
radom模块提供了随机生成对象的方法 Help on module random: NAME random - Random variable generators. FILE /usr/local ...
- linux系统编程之信号(四):alarm和可重入函数
一,alarm() 在将可重入函数之前我们先来了解下alarm()函数使用: #include <unistd.h> unsigned int alarm(unsigned int sec ...
- FLV文件格式官方规范详解
——如果要学习一个新的知识点,官方手册可能是最快的途径.查看网上其他人的总结也许入门更快,但是要准确,深入,完整,还是要看官方手册. 以下内容来自对官方文档Video File Format Sp ...
- ABP框架踩坑记录
ABP框架踩坑记录 ASP.NET Boilerplate是一个专用于现代Web应用程序的通用应用程序框架. 它使用了你已经熟悉的工具,并根据它们实现最佳实践. 文章目录 使用MySQL 配置User ...
- 用 python 修改文件中指定的行数
#! /bin/python filename='setup.ini' lines=[] with open(filename,'r') as f: lines=f.readlines() lines ...
- Weekly Contest 130
1029. Binary Prefix Divisible By 5 Given an array A of 0s and 1s, consider N_i: the i-th subarray fr ...
- zoj4020 Traffic Light(bfs+状态压缩)
题意:每个点有两种状态,0/1,0表示只能上下方向走,1表示只能左右方向走.每走一步整个图的状态改变一次(即0->1,1->0). 数据范围:n,m<=1e15 开始迷之因为数组太大 ...
- [JS深入学习]——数组对象排序
(转) JavaScript实现多维数组.对象数组排序,其实用的就是原生的sort()方法,用于对数组的元素进行排序. sort() 方法用于对数组的元素进行排序.语法如下: arrayObject. ...
- ASP.NET MVC Forms验证机制
ASP.NET MVC 3 使用Forms身份验证 身份验证流程 一.用户登录 1.验证表单:ModelState.IsValid 2.验证用户名和密码:通过查询数据库验证 3.如果用户名和密码正确, ...
- iOS开发证书与配置文件的使用
前提 众所周知,开发iOS应用必须要有iOS证书(Certificates)和配置文件(Provisioning Profiles),那么问题来了: 1.什么是iOS证书,它是如何与app应用关联的? ...