webBrowser代理设置c#代码
程序员文章站
2023-12-09 23:40:34
为webbrowser设置代理: 复制代码 代码如下: public struct struct_internet_proxy_info { public int dwac...
为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);
}
}
*/
复制代码 代码如下:
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);
}
}
*/