Advertisement
Guest User

C# Proxy Setting

a guest
Feb 19th, 2012
1,433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.33 KB | None | 0 0
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.ComponentModel;
  4. using Microsoft.Win32;
  5. using System.Net;
  6.  
  7. namespace PoshHttp
  8. {
  9.     public class Proxies
  10.     {
  11.         public static bool UnsetProxy()
  12.         {
  13.             return SetProxy(null, null, null, null);
  14.         }
  15.         public static bool SetProxy(string strProxy, string username, string password)
  16.         {
  17.             return SetProxy(strProxy, username, password, null);
  18.         }
  19.  
  20.         public static bool SetProxy(string strProxy, string username, string password, string exceptions)
  21.         {
  22.             InternetPerConnOptionList list = new InternetPerConnOptionList();
  23.  
  24.             int optionCount = string.IsNullOrEmpty(strProxy) ? 1 : (string.IsNullOrEmpty(exceptions) ? 2 : 3);
  25.             InternetConnectionOption[] options = new InternetConnectionOption[optionCount];
  26.             // USE a proxy server ...
  27.             options[0].m_Option = PerConnOption.INTERNET_PER_CONN_FLAGS;
  28.             options[0].m_Value.m_Int = (int)((optionCount < 2) ? PerConnFlags.PROXY_TYPE_DIRECT : (PerConnFlags.PROXY_TYPE_DIRECT | PerConnFlags.PROXY_TYPE_PROXY));
  29.             // use THIS proxy server
  30.             if (optionCount > 1)
  31.             {
  32.                 options[1].m_Option = PerConnOption.INTERNET_PER_CONN_PROXY_SERVER;
  33.                 options[1].m_Value.m_StringPtr = Marshal.StringToHGlobalAuto(strProxy);
  34.                 // except for these addresses ...
  35.                 if (optionCount > 2)
  36.                 {
  37.                     options[2].m_Option = PerConnOption.INTERNET_PER_CONN_PROXY_BYPASS;
  38.                     options[2].m_Value.m_StringPtr = Marshal.StringToHGlobalAuto(exceptions);
  39.                 }
  40.             }
  41.  
  42.             // default stuff
  43.             list.dwSize = Marshal.SizeOf(list);
  44.             list.szConnection = IntPtr.Zero;
  45.             list.dwOptionCount = options.Length;
  46.             list.dwOptionError = 0;
  47.  
  48.  
  49.             int optSize = Marshal.SizeOf(typeof(InternetConnectionOption));
  50.             // make a pointer out of all that ...
  51.             IntPtr optionsPtr = Marshal.AllocCoTaskMem(optSize * options.Length);
  52.             // copy the array over into that spot in memory ...
  53.             for (int i = 0; i < options.Length; ++i)
  54.             {
  55.                 IntPtr opt = new IntPtr(optionsPtr.ToInt32() + (i * optSize));
  56.                 Marshal.StructureToPtr(options[i], opt, false);
  57.             }
  58.  
  59.             list.options = optionsPtr;
  60.  
  61.             // and then make a pointer out of the whole list
  62.             IntPtr ipcoListPtr = Marshal.AllocCoTaskMem((Int32)list.dwSize);
  63.             Marshal.StructureToPtr(list, ipcoListPtr, false);
  64.  
  65.             // and finally, call the API method!
  66.             int returnvalue = NativeMethods.InternetSetOption(IntPtr.Zero,
  67.                InternetOption.INTERNET_OPTION_PER_CONNECTION_OPTION,
  68.                ipcoListPtr, list.dwSize) ? -1 : 0;
  69.             if (returnvalue == 0)
  70.             {  // get the error codes, they might be helpful
  71.                 returnvalue = Marshal.GetLastWin32Error();
  72.             }
  73.             // FREE the data ASAP
  74.             Marshal.FreeCoTaskMem(optionsPtr);
  75.             Marshal.FreeCoTaskMem(ipcoListPtr);
  76.             if (returnvalue > 0)
  77.             {  // throw the error codes, they might be helpful
  78.                 throw new Win32Exception(Marshal.GetLastWin32Error());
  79.             }
  80.  
  81.             return (returnvalue < 0);
  82.         }
  83.  
  84.     }
  85.  
  86.     #region WinInet structures
  87.     [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  88.     public struct InternetPerConnOptionList
  89.     {
  90.         public int dwSize;               // size of the INTERNET_PER_CONN_OPTION_LIST struct
  91.         public IntPtr szConnection;         // connection name to set/query options
  92.         public int dwOptionCount;        // number of options to set/query
  93.         public int dwOptionError;           // on error, which option failed
  94.         //[MarshalAs(UnmanagedType.)]
  95.         public IntPtr options;
  96.     };
  97.  
  98.     [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  99.     public struct InternetConnectionOption
  100.     {
  101.         static readonly int Size;
  102.         public PerConnOption m_Option;
  103.         public InternetConnectionOptionValue m_Value;
  104.         static InternetConnectionOption()
  105.         {
  106.             InternetConnectionOption.Size = Marshal.SizeOf(typeof(InternetConnectionOption));
  107.         }
  108.  
  109.         // Nested Types
  110.         [StructLayout(LayoutKind.Explicit)]
  111.         public struct InternetConnectionOptionValue
  112.         {
  113.             // Fields
  114.             [FieldOffset(0)]
  115.             public System.Runtime.InteropServices.ComTypes.FILETIME m_FileTime;
  116.             [FieldOffset(0)]
  117.             public int m_Int;
  118.             [FieldOffset(0)]
  119.             public IntPtr m_StringPtr;
  120.         }
  121.     }
  122.     #endregion
  123.  
  124.     #region WinInet enums
  125.     //
  126.     // options manifests for Internet{Query|Set}Option
  127.     //
  128.     public enum InternetOption : uint
  129.     {
  130.         INTERNET_OPTION_PER_CONNECTION_OPTION = 75
  131.     }
  132.  
  133.     //
  134.     // Options used in INTERNET_PER_CONN_OPTON struct
  135.     //
  136.     public enum PerConnOption
  137.     {
  138.         INTERNET_PER_CONN_FLAGS = 1, // Sets or retrieves the connection type. The Value member will contain one or more of the values from PerConnFlags
  139.         INTERNET_PER_CONN_PROXY_SERVER = 2, // Sets or retrieves a string containing the proxy servers.  
  140.         INTERNET_PER_CONN_PROXY_BYPASS = 3, // Sets or retrieves a string containing the URLs that do not use the proxy server.  
  141.         INTERNET_PER_CONN_AUTOCONFIG_URL = 4//, // Sets or retrieves a string containing the URL to the automatic configuration script.  
  142.     }
  143.  
  144.     //
  145.     // PER_CONN_FLAGS
  146.     //
  147.     [Flags]
  148.     public enum PerConnFlags
  149.     {
  150.         PROXY_TYPE_DIRECT = 0x00000001,  // direct to net
  151.         PROXY_TYPE_PROXY = 0x00000002,  // via named proxy
  152.         PROXY_TYPE_AUTO_PROXY_URL = 0x00000004,  // autoproxy URL
  153.         PROXY_TYPE_AUTO_DETECT = 0x00000008   // use autoproxy detection
  154.     }
  155.     #endregion
  156.  
  157.     internal static class NativeMethods
  158.     {
  159.         [DllImport("WinInet.dll", SetLastError = true, CharSet = CharSet.Auto)]
  160.         [return: MarshalAs(UnmanagedType.Bool)]
  161.         public static extern bool InternetSetOption(IntPtr hInternet, InternetOption dwOption, IntPtr lpBuffer, int dwBufferLength);
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement