C#: How to add proxy support to my program?

Status
Not open for further replies.

kytro360

Member
Original poster
member
Joined
Sep 9, 2009
Posts
376
Reaction score
6
Points
75
Before you say "use the search button" I did. I got the following code but I have an error:

Code:
 public struct Struct_INTERNET_PROXY_INFO
        {
            public int dwAccessType;
            public IntPtr proxy;
            public IntPtr proxyBypass;
        }
        #region "Proxy"

        // The Windows API function that allows us to manipulate
        // IE settings programmatically.

        // The function we will be using to set the proxy settings.
        // ERROR: Not supported in C#: DeclareDeclaration
        private void RefreshIESettings(string strProxy)
        {
            const int INTERNET_OPTION_PROXY = 38;
            const int INTERNET_OPEN_TYPE_PROXY = 3;
            Struct_INTERNET_PROXY_INFO struct_IPI = default(Struct_INTERNET_PROXY_INFO);

            // Filling in structure
            struct_IPI.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
            struct_IPI.proxy = System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi(strProxy);
            struct_IPI.proxyBypass = System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi("local");

            // Allocating memory
            IntPtr intptrStruct = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(System.Runtime.InteropServices.Marshal.SizeOf(struct_IPI));

            // Converting structure to IntPtr
            System.Runtime.InteropServices.Marshal.StructureToPtr(struct_IPI, intptrStruct, true);
            bool iReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY, intptrStruct, System.Runtime.InteropServices.Marshal.SizeOf(struct_IPI));
        
  
        }

        #endregion

The error is with "InternetSetOption" and the error says:The name 'InternetSetOption' does not exist in the current context

Can you help?

bumnp....................
 

sakshi

Member
member
Joined
Jul 3, 2011
Posts
103
Reaction score
10
Points
5
Just to check, are you looking for a proxy for SqlConnection or something like that? Typically apps like this talk to a webservice or something similar rather than making a "native" database connection
 

kytro360

Member
Original poster
member
Joined
Sep 9, 2009
Posts
376
Reaction score
6
Points
75
I got some working code but need help.

Heres the general code for the proxy:

Code:
    public struct Struct_INTERNET_PROXY_INFO
        {
            public int dwAccessType;
            public IntPtr proxy;
            public IntPtr proxyBypass;
        }
        #region "Proxy"
        //  The Windows API function that allows us to manipulate
        //  IE settings programmatically.

        [DllImport("wininet.dll")]
        static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);



        private void RefreshIESettings(string strProxy)
        {
            const int INTERNET_OPTION_PROXY = 38;
            const int INTERNET_OPEN_TYPE_PROXY = 3;
            Struct_INTERNET_PROXY_INFO struct_IPI = default(Struct_INTERNET_PROXY_INFO);

            // Filling in structure
            struct_IPI.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
            struct_IPI.proxy = System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi(strProxy);
            struct_IPI.proxyBypass = System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi("local");

            // Allocating memory
            IntPtr intptrStruct = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(System.Runtime.InteropServices.Marshal.SizeOf(struct_IPI));

            // Converting structure to IntPtr
            System.Runtime.InteropServices.Marshal.StructureToPtr(struct_IPI, intptrStruct, true);
            bool iReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY, intptrStruct, System.Runtime.InteropServices.Marshal.SizeOf(struct_IPI));
        }

        #endregion

Now after researching to use the proxy you need to call RefreshIESettings. My issue is how do I use it with a listbox because Im loading all the proxies in a listbox and want to call it to use a random proxy from the list.

I tried
Code:
 RefreshIESettings(listBoxProxies.Items);
but that didnt work either.
 
Status
Not open for further replies.