zoukankan      html  css  js  c++  java
  • 【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

  • 相关阅读:
    JavaScript是如何工作的:深入类和继承内部原理 + Babel和TypeScript 之间转换
    JavaScript的工作原理:解析、抽象语法树(AST)+ 提升编译速度5个技巧
    如何保证MongoDB的安全性?
    SQLSERVER 中sp_who, sp_who2和sp_who3(转载)
    exec sp_spaceused如何只返回一个结果集(转载)
    c#静态构造函数 与 构造函数 你是否还记得?(转载)
    如何让.NET Core支持GB2312和GBK
    C#中用HttpWebRequest中发送GET/HTTP/HTTPS请求 (转载)
    ASP.NET Core读取AppSettings (转载)
    使用自定义端口连接SQL Server的方法(转载)
  • 原文地址:https://www.cnblogs.com/lyghost/p/3222961.html
Copyright © 2011-2022 走看看