• 【C#】.NET中设置代理服务器浏览网页的实现--转载


    目前很多种类的浏览器中都有代理服务器的设置,用户可以通过浏览器自定义更换自己的IP,实现在线代理翻(河蟹)墙浏览网页。

     

     

    而在.NET中,亦可以通过调用API函数InternetSetOption来实现自定义代理IP的设置。。

     

    首先引用System.Runtime.InteropServices名字空间:

    using System.Runtime.InteropServices;

     

    接着引入"wininet.dll"库文件,并定义IP代理设置方法:

     1 #region 在线代理
     2         public struct Struct_INTERNET_PROXY_INFO
     3         {
     4             public int dwAccessType;
     5             public IntPtr proxy;
     6             public IntPtr proxyBypass;
     7         };
     8         /// <summary>
     9         /// 定义API函数
    10         /// </summary>
    11         /// <param name="hInternet"></param>
    12         /// <param name="dwOption"></param>
    13         /// <param name="lpBuffer"></param>
    14         /// <param name="lpdwBufferLength"></param>
    15         /// <returns></returns>
    16         [DllImport("wininet.dll", SetLastError = true)]
    17         private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);
    18 
    19         /// <summary>
    20         /// 刷新代理IP设置
    21         /// </summary>
    22         /// <param name="strProxy"></param>
    23         private void RefreshIESettings(string strProxy)
    24         {
    25             const int INTERNET_OPTION_PROXY = 38;
    26             const int INTERNET_OPEN_TYPE_PROXY = 3;
    27 
    28             Struct_INTERNET_PROXY_INFO struct_IPI;
    29 
    30             // Filling in structure 
    31             struct_IPI.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
    32             struct_IPI.proxy = Marshal.StringToHGlobalAnsi(strProxy);
    33             struct_IPI.proxyBypass = Marshal.StringToHGlobalAnsi("local");
    34 
    35             // Allocating memory 
    36             IntPtr intptrStruct = Marshal.AllocCoTaskMem(Marshal.SizeOf(struct_IPI));
    37 
    38             // Converting structure to IntPtr 
    39             Marshal.StructureToPtr(struct_IPI, intptrStruct, true);
    40 
    41             bool iReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY, intptrStruct, Marshal.SizeOf(struct_IPI));
    42         }
    43 
    44         /// <summary>
    45         /// 在线代理访问网页URL
    46         /// </summary>
    47         /// <param name="ip">代理IP</param>
    48         /// <param name="port">代理端口</param>
    49         /// <param name="url">要访问的网页URL</param>
    50         private void NaviByProxy(string ip, string port, string url)
    51         {
    52             ListViewItem item = this.lst.SelectedItems[0];
    53             //this.listView_usr.Items.Remove(item);
    54 
    55             RefreshIESettings(string.Format("{0}:{1}", ip, port));
    56 
    57             System.Object nullObject = 0;
    58             string strTemp = String.Empty;
    59             System.Object nullObjStr = strTemp;
    60             this.wbCnblog.Navigate(url);
    61         }
    62         
    63         #endregion
    64  
    65 调用NaviByProxy该方法,代理浏览网页:
    66                 if (lst.SelectedItems.Count > 0)
    67                 {
    68                     //代理游览网站URL
    69                     NaviByProxy(
    70                         lst.SelectedItems[0].SubItems[0].Text, //选中的代理IP地址
    71                         lst.SelectedItems[0].SubItems[1].Text, //选中的代理IP的端口
    72                         textBox_url.Text.Trim()//url地址
    73                         );
    74                 }
    75                 else wbCnblog.Navigate(textBox_url.Text);

    实际效果:

     

    转自:http://www.189works.com/article-41653-1.html

  • 相关阅读:
    BT协议分析(1)—1.0协议
    Qt线程(2) QThread中使用WorkObject
    新浪微博的开放平台官方文档太粗略,记:仿大平台来实现
    58同城 骗子太多
    代码实现业务经验(程序员的核心能力)
    gitbash 本地文件提交为一个新的项目 到 gitlab
    Spring 核心容器 IOC
    spring AOP 理解
    java不返回某些字段,包括 null
    CentOS7安装 Redis5 单实例
  • 原文地址:https://www.cnblogs.com/lyghost/p/3222961.html
Copyright © 2020-2023  润新知