WebBrowser Proxy set C # code _c# tutorial

Source: Internet
Author: User
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);
}
}
*/
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.