C# webBrowser 使用ip代理访问网站
在做一些网站模拟操作或者用爬虫不断爬取同一个网站的数据时,有时候目标网站会设置一些防爬取策略,同一ip多次访问则会直接拦截,那么如果遇到这种情况我们如何绕过呢? 答案显而易见直接换ip。最近用到了这方面的东西所以研究了一下,想着还是写个博客存一下,以防后面还会用到类似的功能。
效果如下:
代码如下:
#region IP代理代码块
public struct Struct_INTERNET_PROXY_INFO
{
public int dwAccessType;
public IntPtr proxy;
public IntPtr proxyBypass;
};
//strProxy为代理IP:端口
private void RefreshIESettings(string strProxy)
{
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));
}
[DllImport("wininet.dll", SetLastError = true)]
private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);
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);
}
}
#endregion
使用教程:
private void button8_Click(object sender, EventArgs e)
{
string proxy = this.textBox1.Text;
RefreshIESettings(proxy);
IEProxy ie = new IEProxy(proxy);
ie.RefreshIESettings().ToString();
}
private void button9_Click(object sender, EventArgs e)
{
IEProxy ie = new IEProxy(null);
ie.DisableIEProxy();
}
private void button10_Click(object sender, EventArgs e)
{
webBrowser1.Navigate("http://ie.icoa.cn/", null,null,null);
}
注: textBox1.Text 代表的是代理ip+端口,格式为103.217.179.185:3128
版权声明:
作者:兴兴
文章:C# webBrowser 使用ip代理访问网站
链接:https://www.networkcabin.com/notes/2520
文章版权归本站所有,未经授权请勿转载。
作者:兴兴
文章:C# webBrowser 使用ip代理访问网站
链接:https://www.networkcabin.com/notes/2520
文章版权归本站所有,未经授权请勿转载。
THE END