【C#】.NET中设置代理服务器浏览网页的实现--转载
2024-10-19 00:21:19
目前很多种类的浏览器中都有代理服务器的设置,用户可以通过浏览器自定义更换自己的IP,实现在线代理翻(河蟹)墙浏览网页。
而在.NET中,亦可以通过调用API函数InternetSetOption来实现自定义代理IP的设置。。
首先引用System.Runtime.InteropServices名字空间:
using System.Runtime.InteropServices;
接着引入"wininet.dll"库文件,并定义IP代理设置方法:
#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 = ;
const int INTERNET_OPEN_TYPE_PROXY = ; 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[];
//this.listView_usr.Items.Remove(item); RefreshIESettings(string.Format("{0}:{1}", ip, port)); System.Object nullObject = ;
string strTemp = String.Empty;
System.Object nullObjStr = strTemp;
this.wbCnblog.Navigate(url);
} #endregion 调用NaviByProxy该方法,代理浏览网页:
if (lst.SelectedItems.Count > )
{
//代理游览网站URL
NaviByProxy(
lst.SelectedItems[].SubItems[].Text, //选中的代理IP地址
lst.SelectedItems[].SubItems[].Text, //选中的代理IP的端口
textBox_url.Text.Trim()//url地址
);
}
else wbCnblog.Navigate(textBox_url.Text);
实际效果:
转自:http://www.189works.com/article-41653-1.html
最新文章
- BZOJ 2079: [Poi2010]Guilds
- 工厂模式 - Factory
- tangram2.6(XE2)\framework框架加载包异常 调试的地方
- SpringMVC的几种返回方式
- Java——Selector
- 如何开启SQL Server 2008的远程联机
- A Simple Problem with Integers(树状数组HDU4267)
- 手把手教学:详解HTML5移动开发框架PhoneJS
- (转)jQuery Validation Plugin客户端表单证验插件
- TodoList开发笔记 – PartⅠ
- Java之JSP基础语法
- 找到你在网页中缓存起来的flash文件
- SHELL希尔排序
- Linux下学习摄像头使用
- sql脚本练习
- <;table>;居中的一种方法
- Base64 转 图片
- Java连接S3并上传Redis
- 禁用gridview,listview回弹或下拉悬停
- python安装新版本及pip
热门文章
- JQUERY1.9学习笔记 之基本过滤器(七) 语言选择器
- $(this).val()与this.value的区别?text()与html()的区别?
- Js正则表达式提取图片地址
- php explode 用法详解
- 下拉列表框Combo Box
- Flume笔记--source端监听目录,sink端上传到HDFS
- window.onscroll
- c++四则运算代码
- 对编程语言的需求总结为四个:效率,灵活,抽象,生产率(C++玩的是前三个,Java和C#玩的是后两个)
- YII 事件event和行为Behavior