WebBrowser proxy Setting c # code
Last Update:2018-12-08
Source: Internet
Author: User
Set proxy for webBrowser: Copy code The Code is 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 is the proxy IP Address: 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: xx ");
Perfect method:
/* 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 proxy
Public bool RefreshIESettings ()
{
Return InternetSetOption (this. ProxyStr );
}
// Cancel proxy
Public bool DisableIEProxy ()
{
Return InternetSetOption (string. Empty );
}
}
*/