C# WebBrowser 设置代理完全解决方案

4 篇文章 0 订阅

转载:https://www.cnblogs.com/goody9807/archive/2011/09/21/2183089.html

 

微软webbrowser控件也就是IE插件,他的所有功能就像IE类似,当然设置也是一样的,下面介绍下webbrowser如何设置代理,可不要用这个对抗广告联盟哦

You can change the proxy with InternetSetOption method from the wininet.dll, here is a example to set the proxy:

using System.Runtime.InteropServices;

 

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) 

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)); 


private void SomeFunc() 

RefreshIESettings("192.168.1.200:1010"); 

System.Object nullObject = 0; 
string strTemp = String.Empty; 
System.Object nullObjStr = strTemp;
axWebBrowser1.Navigate("http://willstay.tripod.com", ref nullObject, ref nullObjStr, ref nullObjStr, ref nullObjStr); 
}

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

昨 天做的投票机遇到个新问题,昨天开始那个投票开始现在ip地址,每个地址只能投5票/天。如果每次更改ie的连接为代理服务器,那也麻烦死了,如 果改用webclient,那昨天的2个多小时就白费了,上网一通狂收还真找到了办法,这下好办了,建了一个proxy.txt文档,里面放上从网上收到 的代理服务器,然后程序读到一个listbox里面,每次需要更换ip的时候只要单击一次,就可以还一个地址重新投票了。
附上proxy.cs

using System.Runtime.InteropServices;//需要添加这个引用
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);
public 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
py.proxy py1 = new proxy();
py1.RefreshIESettings(”221.4.155.51:3128″);
System.Object nullObject = 0;
string strTemp = String.Empty;
System.Object nullObjStr = strTemp;
webBrowser1.Navigate(”http://www.hfxsy.cn”, null, null, null);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值