【C#】.NET中设置代理服务器浏览网页的实现
目前很多种类的浏览器中都有代理服务器的设置,用户可以通过浏览器自定义更换自己的IP,实现在线代理翻(河蟹)墙浏览网页。
而在.NET中,亦可以通过调用API函数InternetSetOption来实现自定义代理IP的设置。。
首先引用System.Runtime.InteropServices名字空间:
using System.Runtime.InteropServices;
接着引入"wininet.dll"库文件,并定义IP代理设置方法:
View Code
#region 在线代理
public struct Struct_INTERNET_PROXY_INFO
{
public int dwAccessType;
public IntPtr proxy;
public IntPtr proxyBypass;
};
/// <summary>
/// 定义API函数
/// </summary>
/// <param name="hInternet"></param>
/// <param name="dwOption"></param>
/// <param name="lpBuffer"></param>
/// <param name="lpdwBufferLength"></param>
/// <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));
}
/// <summary>
/// 在线代理访问网页URL
/// </summary>
/// <param name="ip">代理IP</param>
/// <param name="port">代理端口</param>
/// <param name="url">要访问的网页URL</param>
private void NaviByProxy(string ip, string port, string url)
{
ListViewItem item = this.lst.SelectedItems[0];
//this.listView_usr.Items.Remove(item);
RefreshIESettings(string.Format("{0}:{1}", ip, port));
System.Object nullObject = 0;
string strTemp = String.Empty;
System.Object nullObjStr = strTemp;
this.wbCnblog.Navigate(url);
}
#endregion
public struct Struct_INTERNET_PROXY_INFO
{
public int dwAccessType;
public IntPtr proxy;
public IntPtr proxyBypass;
};
/// <summary>
/// 定义API函数
/// </summary>
/// <param name="hInternet"></param>
/// <param name="dwOption"></param>
/// <param name="lpBuffer"></param>
/// <param name="lpdwBufferLength"></param>
/// <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));
}
/// <summary>
/// 在线代理访问网页URL
/// </summary>
/// <param name="ip">代理IP</param>
/// <param name="port">代理端口</param>
/// <param name="url">要访问的网页URL</param>
private void NaviByProxy(string ip, string port, string url)
{
ListViewItem item = this.lst.SelectedItems[0];
//this.listView_usr.Items.Remove(item);
RefreshIESettings(string.Format("{0}:{1}", ip, port));
System.Object nullObject = 0;
string strTemp = String.Empty;
System.Object nullObjStr = strTemp;
this.wbCnblog.Navigate(url);
}
#endregion
调用NaviByProxy该方法,代理浏览网页:
if (lst.SelectedItems.Count > 0)
{
//代理游览网站URL
NaviByProxy(
lst.SelectedItems[0].SubItems[0].Text, //选中的代理IP地址
lst.SelectedItems[0].SubItems[1].Text, //选中的代理IP的端口
textBox_url.Text.Trim()//url地址
);
}
else wbCnblog.Navigate(textBox_url.Text);
{
//代理游览网站URL
NaviByProxy(
lst.SelectedItems[0].SubItems[0].Text, //选中的代理IP地址
lst.SelectedItems[0].SubItems[1].Text, //选中的代理IP的端口
textBox_url.Text.Trim()//url地址
);
}
else wbCnblog.Navigate(textBox_url.Text);
实际效果:
源码: