C# 修改浏览器代理地址
参考地址 https://www.cnblogs.com/skybreak/p/3554229.html
调用方式为 :
IEProxy.SetProxy(strProxy);
可用的免费代理网站:
下方为修改代理的类:
namespace CatchDataFrame.Utils
{
public class IEProxy
{
public static bool UnsetProxy()
{
return SetProxy(null, null);
}
public static bool SetProxy(string strProxy)
{
return SetProxy(strProxy, null);
}
public static bool SetProxy(string strProxy, string exceptions)
{
InternetPerConnOptionList list = new InternetPerConnOptionList();
int optionCount = string.IsNullOrEmpty(strProxy) ? 1 : (string.IsNullOrEmpty(exceptions) ? 2 : 3);
InternetConnectionOption[] options = new InternetConnectionOption[optionCount];
//use a proxy server
options[0].M_Option = PerConnOption.INTERNET_PER_CONN_FLAGS;
options[0].M_Value.M_Int = (int)((optionCount < 2) ? PerConnFlags.PROXY_TYPE_DIRECT :
(PerConnFlags.PROXY_TYPE_DIRECT | PerConnFlags.PROXY_TYPE_PROXY));
//use this proxy server
if(optionCount>1)
{
options[1].M_Option = PerConnOption.INTERNET_PER_CONN_PROXY_SERVE;
options[1].M_Value.M_StringPtr = Marshal.StringToHGlobalAuto(strProxy);
//except for these addresses
if(optionCount>2)
{
options[2].M_Option = PerConnOption.INTERNET_PER_CONN_PROXY_BYPASS;
options[2].M_Value.M_StringPtr = Marshal.StringToHGlobalAuto(exceptions);
}
}
//default stuff
list.DwSize = Marshal.SizeOf(list);
list.SzConnection = IntPtr.Zero;
list.DwOptionCount = options.Length;
list.DwOptionError = 0;
int optSize = Marshal.SizeOf(typeof(InternetConnectionOption));
//make a pointer out of all that
IntPtr optionPtr = Marshal.AllocCoTaskMem(optSize * options.Length);
//copy the array over into that spot in memory
for(int i=0;i<options.Length;++i)
{
IntPtr opt = new IntPtr(optionPtr.ToInt32() + (i * optSize));
Marshal.StructureToPtr(options[i], opt, false);
}
list.Options = optionPtr;
//and then make a pointer out of the whole list
IntPtr ipcoListPtr = Marshal.AllocCoTaskMem((Int32)list.DwSize);
Marshal.StructureToPtr(list, ipcoListPtr, false);
//finally,call the api mehod
int returnvalue = NativeMethods.InternetSetOption(IntPtr.Zero,
InternetOption.INTERNET_OPTION_PER_CONNECTION_OPTION,
ipcoListPtr, list.DwSize) ? -1 : 0;
if (returnvalue == 0)
{
//get the error codes,they might be helpful
throw new Win32Exception(Marshal.GetLastWin32Error());
}
return (returnvalue < 0);
}
}
#region WinInet structures
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct InternetPerConnOptionList
{
public int DwSize; //size of the INTERNET_PER_CONN_OPTION_LIST struct
public IntPtr SzConnection; //connection name to set/query options
public int DwOptionCount; //number of options to set/query
public int DwOptionError; //on error ,which option failed
public IntPtr Options;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct InternetConnectionOption
{
static readonly int Size;
public PerConnOption M_Option;
public InternetConnectionOptionValue M_Value;
static InternetConnectionOption()
{
InternetConnectionOption.Size = Marshal.SizeOf(typeof(InternetConnectionOption));
}
//Nested types
[StructLayout(LayoutKind.Explicit)]
public struct InternetConnectionOptionValue
{
[FieldOffset(0)]
public System.Runtime.InteropServices.ComTypes.FILETIME M_FileTime;
[FieldOffset(0)]
public int M_Int;
[FieldOffset(0)]
public IntPtr M_StringPtr;
}
}
#endregion
#region WinInet enums
/// <summary>
/// options manifests for Internet{Query|Set} Option
/// </summary>
public enum InternetOption : uint
{
INTERNET_OPTION_PER_CONNECTION_OPTION = 75
}
/// <summary>
/// Options used in INTERNET_PER_CONN_OPTION struct
/// </summary>
public enum PerConnOption
{
/// <summary>
/// Sets or retrieves the connection type.The vlaue member will contain one or more of the values from PerConnFlags
/// </summary>
INTERNET_PER_CONN_FLAGS = 1,
/// <summary>
/// Sets or retrieves a string containing the proxy servers
/// </summary>
INTERNET_PER_CONN_PROXY_SERVE = 2,
/// <summary>
/// Sets or retrieves a string containing the urls what do not user the proxy server
/// </summary>
INTERNET_PER_CONN_PROXY_BYPASS = 3,
/// <summary>
/// Sets or retrieves a string containing the url to the automatic configuration script
/// </summary>
INTERNET_PER_CONN_AUTOCONFIG_URL = 4
}
/// <summary>
/// PER_CONN_FLAGS
/// </summary>
[Flags]
public enum PerConnFlags
{
PROXY_TYPE_DIRECT = 0x00000001, //direct to net
PROXY_TYPE_PROXY = 0x00000002, //via named proxy
PROXY_TYPE_AUTO_PROXY_URL = 0x00000004, //autoproxy url
PROXY_TYPE_AUTO_DETECT = 0x00000008 // use autoproxy detection
}
#endregion
internal static class NativeMethods
{
[DllImport("WinInet.dll", SetLastError = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool InternetSetOption(IntPtr hInternet, InternetOption dwOption, IntPtr lpBuffer, int dwBufferLength);
}
}
查验结果:
可通过各浏览器的设置-->代理-->连接-->局域网设置。查看修改后的代理地址。