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:// ...
随机推荐
- spring boot 使用 HandlerInterceptor
# 背景 在实际项目中,接口出于安全考虑,都会有验签的计算.目前接触的项目来看基本都是时间戳+干扰因子 然后md5计算的方式.现在学习,写一个简单demo, 其实如果不引入拦截器的话,验签计算全部在c ...
- jquery批量控制表单元素
网上查了很久,避免下次再遇到相同的问题,记录一下: $("form input").prop("readonly", true); $("form i ...
- ASP.Net Core 2.2 InProcess托管的Bug:unable to open database file
最近把项目更新到了ASP.Net Core 2.2,发布之后发现在IIS下使用SQLite数据库不行了,报异常说不能打开数据库."unable to open database file&q ...
- svn 连接超时,连接失败解决办法
1.确认服务是否开启 2.Windows防火墙是否开启,如开启则关闭防火墙 3.安全软件是否将3306与443端口关闭. 关闭后无法连接
- [Xamarin]我的Xamarin填坑之旅(一)
一想到明天是星期五,不对,是今天,心里就很激动,毕竟明天没课.激动之余,来写一篇博客,记录一下最近踏坑Xamarin开发校园助手APP的一些事儿.也许更像是一篇流水账. 在扯Xamarin之前,有必要 ...
- 面试题-一个列表向右移动k位
def sort(lst,k): length = len(lst) left =lst[:-k] right =lst[-k:] lst.clear() lst.extend(right) lst. ...
- With语句上下文管理
在平时工作中总会有这样的任务,它们需要开始前做准备,然后做任务,然后收尾清理....比如读取文件,需要先打开,读取,关闭 这个时候就可以使用with简化代码,很方便 1.没有用with语句 1 2 3 ...
- Day 15 内置函数 , 匿名函数.
1. 最大值 max,最小值# #最大值 ret = max(1,2,-3)print(ret)# 结果 2ret=max([1,2,3,4])print(ret)# 结果 4 2.sum 函数用法 ...
- python numpy 数组中元素大于等于0的元素
>>> import numpy as np >>> a = np.random.randint(-5, 5, (5, 5)) >>> a arr ...
- centos 7 初始化脚本
#!/bin/bash # 时间: 2018-11-21 # 作者: HuYuan # 描述: CentOS 7 初始化脚本 # 加载配置文件 if [ -n "${1}" ];t ...