C#webbrowser How to use a proxy server WinForm

Source: Internet
Author: User

In fact, the use of WebBrowser in C # Everyone should be, the forum also has a lot of examples before you can find out
But the way to set up proxies just like a browser can be a lot of people don't know yet.
This is actually called a DLL file to set,
Now let's see it with me.
First of all, we must first build a structure that is the proxy information structure
As follows

[C #]Plain Text view copy code ?
010203040506070809 /// <summary>   /// 代理结构体   /// </summary>   public struct Struct_INTERNET_PROXY_INFO   {       public int dwAccessType;       public IntPtr proxy;//IP以及端口号       public IntPtr proxyBypass;   };


Here's how to set up a specific implementation of the agent

[C #]Plain Text view copy code ?
01020304050607080910111213141516171819202122232425262728293031 /// <summary>        /// 设置代理的Api        /// </summary>        /// <returns></returns>        [DllImport("wininet.dll", SetLastError = true)]        private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);        /// <summary>        /// 代理IP以及端口号        /// </summary>        /// <param name="strProxy"></param>        private void RefreshIESettings(string strProxy)        {            const int INTERNET_OPTION_PROXY = 38;            const int INTERNET_OPEN_TYPE_PROXY = 3;            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));            // Converting structure to IntPtr             Marshal.StructureToPtr(struct_IPI, intptrStruct, true);             bool iReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY, intptrStruct, Marshal.SizeOf(struct_IPI));        }


It's very easy to use.

[C #]Plain Text view copy code ?
0102 RefreshIESettings("41.129.53.227:80");webBrowser1.Navigate("http://www.sufeinet.com");


That's all you can do.
All right, everybody, try it yourself.

C#webbrowser How to use a proxy server WinForm

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.