To set up an agent for WebBrowser:
Copy Code code as follows:
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 for proxy IP: port
{
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));
}
Use: Refreshiesettings ("xxx.xxx.xxx.xxx:xx");
Perfect way:
/* Complete parsing
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;
}
Set up agents
public bool Refreshiesettings ()
{
Return InternetSetOption (this. PROXYSTR);
}
Cancel Agent
public bool Disableieproxy ()
{
Return InternetSetOption (String. Empty);
}
}
*/