INTERNET_OPEN_TYPE.cs
namespace CSWebBrowserWithProxy
{
public enum INTERNET_OPEN_TYPE
{
INTERNET_OPEN_TYPE_PRECONFIG = 0,
INTERNET_OPEN_TYPE_DIRECT = 1,
INTERNET_OPEN_TYPE_PROXY=3,
INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY =4
}
}
INTERNET_OPTION.cs
namespace CSWebBrowserWithProxy
{
public enum INTERNET_OPTION
{
INTERNET_OPTION_PER_CONNECTION_OPTION = 75,
INTERNET_OPTION_SETTINGS_CHANGED = 39,
INTERNET_OPTION_REFRESH = 37
}
}
INTERNET_PER_CONN_OPTION.cs
using System.Runtime.InteropServices;
namespace CSWebBrowserWithProxy
{
public enum INTERNET_PER_CONN_OptionEnum
{
INTERNET_PER_CONN_FLAGS = 1,
INTERNET_PER_CONN_PROXY_SERVER = 2,
INTERNET_PER_CONN_PROXY_BYPASS = 3,
INTERNET_PER_CONN_AUTOCONFIG_URL = 4,
INTERNET_PER_CONN_AUTODISCOVERY_FLAGS = 5,
INTERNET_PER_CONN_AUTOCONFIG_SECONDARY_URL = 6,
INTERNET_PER_CONN_AUTOCONFIG_RELOAD_DELAY_MINS = 7,
INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_TIME = 8,
INTERNET_PER_CONN_AUTOCONFIG_LAST_DETECT_URL = 9,
INTERNET_PER_CONN_FLAGS_UI = 10
}
public enum INTERNET_OPTION_PER_CONN_FLAGS
{
PROXY_TYPE_DIRECT = 0x00000001,
PROXY_TYPE_PROXY = 0x00000002,
PROXY_TYPE_AUTO_PROXY_URL = 0x00000004,
PROXY_TYPE_AUTO_DETECT = 0x00000008
}
[StructLayout(LayoutKind.Explicit)]
public struct INTERNET_PER_CONN_OPTION_OptionUnion
{
[FieldOffset(0)]
public int dwValue;
[FieldOffset(0)]
public System.IntPtr pszValue;
[FieldOffset(0)]
public System.Runtime.InteropServices.ComTypes.FILETIME ftValue;
}
[StructLayout(LayoutKind.Sequential)]
public struct INTERNET_PER_CONN_OPTION
{
public int dwOption;
public INTERNET_PER_CONN_OPTION_OptionUnion Value;
}
}
INTERNET_PER_CONN_OPTION_LIST.cs
using System.Runtime.InteropServices;
namespace CSWebBrowserWithProxy
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct INTERNET_PER_CONN_OPTION_LIST
{
public int Size;
public System.IntPtr Connection;
public int OptionCount;
public int OptionError;
public System.IntPtr pOptions;
}
}
InternetProxy.cs
using System.Collections.Generic;
using System.Xml.Linq;
namespace CSWebBrowserWithProxy
{
public class InternetProxy
{
public string ProxyName { get; set; }
public string Address { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public override bool Equals(object obj)
{
if (obj is InternetProxy)
{
InternetProxy proxy = obj as InternetProxy;
return this.Address.Equals(proxy.Address, System.StringComparison.OrdinalIgnoreCase)
&& this.UserName.EndsWith(proxy.UserName, System.StringComparison.OrdinalIgnoreCase)
&& this.Password.Equals(proxy.Password, System.StringComparison.Ordinal);
}
else
{
return false;
}
}
public override int GetHashCode()
{
return this.Address.GetHashCode()
+this.UserName.GetHashCode()
+this.Password.GetHashCode();
}
public static readonly InternetProxy NoProxy = new InternetProxy
{
Address = string.Empty,
Password = string.Empty,
ProxyName = string.Empty,
UserName = string.Empty
};
static List<InternetProxy> proxyList = null;
public static List<InternetProxy> ProxyList
{
get
{
// Gets the proxy servers in ProxyList.xml.
if (proxyList == null)
{
proxyList = new List<InternetProxy>();
try
{
XElement listXml = XElement.Load("ProxyList.xml");
foreach (var proxy in listXml.Elements("Proxy"))
{
proxyList.Add(
new InternetProxy
{
ProxyName = proxy.Element("ProxyName").Value,
Address = proxy.Element("Address").Value,
UserName = proxy.Element("UserName").Value,
Password = proxy.Element("Password").Value
});
}
}
catch (System.Exception)
{
}
}
return proxyList;
}
}
}
}
WinINet.cs
using System;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace CSWebBrowserWithProxy
{
public static class WinINet
{
static string agent = Process.GetCurrentProcess().ProcessName;
public static bool SetConnectionProxy(bool isMachineSetting, string proxyServer)
{
if (isMachineSetting)
{
return SetConnectionProxy(null, proxyServer);
}
else
{
return SetConnectionProxy(agent, proxyServer);
}
}
public static bool SetConnectionProxy(string agentName, string proxyServer)
{
IntPtr hInternet = IntPtr.Zero;
try
{
if (!string.IsNullOrEmpty(agentName))
{
hInternet = NativeMethods.InternetOpen(
agentName,
(int)INTERNET_OPEN_TYPE.INTERNET_OPEN_TYPE_DIRECT,
null,
null,
0);
}
return SetConnectionProxyInternal(hInternet, proxyServer);
}
finally
{
if (hInternet != IntPtr.Zero)
{
NativeMethods.InternetCloseHandle(hInternet);
}
}
}
static bool SetConnectionProxyInternal(IntPtr hInternet, string proxyServer)
{
INTERNET_PER_CONN_OPTION[] Options = new INTERNET_PER_CONN_OPTION[3];
Options[0] = new INTERNET_PER_CONN_OPTION();
Options[0].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_FLAGS;
Options[0].Value.dwValue = (int)INTERNET_OPTION_PER_CONN_FLAGS.PROXY_TYPE_PROXY;
Options[1] = new INTERNET_PER_CONN_OPTION();
Options[1].dwOption =
(int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_SERVER;
Options[1].Value.pszValue = Marshal.StringToHGlobalAnsi(proxyServer);
Options[2] = new INTERNET_PER_CONN_OPTION();
Options[2].dwOption =
(int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_BYPASS;
Options[2].Value.pszValue = Marshal.StringToHGlobalAnsi("local");
System.IntPtr buffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(Options[0])
+ Marshal.SizeOf(Options[1]) + Marshal.SizeOf(Options[2]));
System.IntPtr current = buffer;
for (int i = 0; i < Options.Length; i++)
{
Marshal.StructureToPtr(Options[i], current, false);
current = (System.IntPtr)((int)current + Marshal.SizeOf(Options[i]));
}
INTERNET_PER_CONN_OPTION_LIST option_list = new INTERNET_PER_CONN_OPTION_LIST();
option_list.pOptions = buffer;
option_list.Size = Marshal.SizeOf(option_list);
option_list.Connection = IntPtr.Zero;
option_list.OptionCount = Options.Length;
option_list.OptionError = 0;
int size = Marshal.SizeOf(option_list);
IntPtr intptrStruct = Marshal.AllocCoTaskMem(size);
Marshal.StructureToPtr(option_list, intptrStruct, true);
bool bReturn = NativeMethods.InternetSetOption(
hInternet,
INTERNET_OPTION.INTERNET_OPTION_PER_CONNECTION_OPTION,
intptrStruct, size);
Marshal.FreeCoTaskMem(buffer);
Marshal.FreeCoTaskMem(intptrStruct);
if (!bReturn)
{
throw new ApplicationException(" Set Internet Option Failed!");
}
NativeMethods.InternetSetOption(
hInternet,
INTERNET_OPTION.INTERNET_OPTION_SETTINGS_CHANGED,
IntPtr.Zero, 0);
NativeMethods.InternetSetOption(
hInternet,
INTERNET_OPTION.INTERNET_OPTION_REFRESH,
IntPtr.Zero, 0);
return bReturn;
}
public static INTERNET_PER_CONN_OPTION_LIST GetSystemProxy()
{
INTERNET_PER_CONN_OPTION[] Options = new INTERNET_PER_CONN_OPTION[3];
Options[0] = new INTERNET_PER_CONN_OPTION();
Options[0].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_FLAGS;
Options[1] = new INTERNET_PER_CONN_OPTION();
Options[1].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_SERVER;
Options[2] = new INTERNET_PER_CONN_OPTION();
Options[2].dwOption = (int)INTERNET_PER_CONN_OptionEnum.INTERNET_PER_CONN_PROXY_BYPASS;
System.IntPtr buffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(Options[0])
+ Marshal.SizeOf(Options[1]) + Marshal.SizeOf(Options[2]));
System.IntPtr current = (System.IntPtr)buffer;
for (int i = 0; i < Options.Length; i++)
{
Marshal.StructureToPtr(Options[i], current, false);
current = (System.IntPtr)((int)current + Marshal.SizeOf(Options[i]));
}
INTERNET_PER_CONN_OPTION_LIST Request = new INTERNET_PER_CONN_OPTION_LIST();
Request.pOptions = buffer;
Request.Size = Marshal.SizeOf(Request);
Request.Connection = IntPtr.Zero;
Request.OptionCount = Options.Length;
Request.OptionError = 0;
int size = Marshal.SizeOf(Request);
bool result = NativeMethods.InternetQueryOption(
IntPtr.Zero,
INTERNET_OPTION.INTERNET_OPTION_PER_CONNECTION_OPTION,
ref Request,
ref size);
if (!result)
{
throw new ApplicationException("Get System Internet Option Failed! ");
}
return Request;
}
public static bool RestoreSystemProxy()
{
return RestoreSystemProxy(agent);
}
public static bool RestoreSystemProxy(string agentName)
{
if (string.IsNullOrEmpty(agentName))
{
throw new ArgumentNullException("Agent name cannot be null or empty!");
}
IntPtr hInternet = IntPtr.Zero;
try
{
if (!string.IsNullOrEmpty(agentName))
{
hInternet = NativeMethods.InternetOpen(
agentName,
(int)INTERNET_OPEN_TYPE.INTERNET_OPEN_TYPE_DIRECT,
null,
null,
0);
}
return RestoreSystemProxyInternal(hInternet);
}
finally
{
if (hInternet != IntPtr.Zero)
{
NativeMethods.InternetCloseHandle(hInternet);
}
}
}
static bool RestoreSystemProxyInternal(IntPtr hInternet)
{
var request = GetSystemProxy();
int size = Marshal.SizeOf(request);
IntPtr intptrStruct = Marshal.AllocCoTaskMem(size);
Marshal.StructureToPtr(request, intptrStruct, true);
bool bReturn = NativeMethods.InternetSetOption(
hInternet,
INTERNET_OPTION.INTERNET_OPTION_PER_CONNECTION_OPTION,
intptrStruct,
size);
Marshal.FreeCoTaskMem(request.pOptions);
Marshal.FreeCoTaskMem(intptrStruct);
if (!bReturn)
{
throw new ApplicationException(" Set Internet Option Failed! ");
}
NativeMethods.InternetSetOption(
hInternet,
INTERNET_OPTION.INTERNET_OPTION_SETTINGS_CHANGED,
IntPtr.Zero,
0);
NativeMethods.InternetSetOption(
hInternet,
INTERNET_OPTION.INTERNET_OPTION_REFRESH,
IntPtr.Zero,
0);
return bReturn;
}
}
}
1 comments:
Do anyone pattern this website you or perhaps did you actually hire an attorney to get the pirate proxy done for yourself? Plz interact since I!|m seeking to pattern my own site in addition to would wish to learn where u became this kind of by.
Post a Comment