C# 設定代理

來源:互聯網
上載者:User

標籤:blog   http   使用   os   for   io   

1.檢查代理是否可用:

  /// <summary>        ///  驗證代理IP地址是否可用       /// </summary>       /// <param name="callback">是否可用</param>       /// <param name="ip">地址</param>       /// <param name="port">連接埠</param>       public void ChecKedForIP(Action<bool> callback,string ip,int port,int timeout=0)       {           try           {               string Reshtml = string.Empty;//請求返回的結果               WebProxy webproxy = new WebProxy(ip, port);  //執行個體代理設定               HttpWebRequest Httpweq = (HttpWebRequest)WebRequest.Create("http://www.baidu.com");               Httpweq.Proxy = webproxy;//類比設定代理請求               HttpWebResponse HttpRespon = (HttpWebResponse)Httpweq.GetResponse();               Httpweq.Timeout = timeout != 0 ? timeout : 10000; //預設10秒,如果設定值,就用設定的值               Encoding encoding = Encoding.GetEncoding("gb2312");//設定編碼格式為漢字,防止英文系統,無法解碼               using (StreamReader reader = new StreamReader(HttpRespon.GetResponseStream(), encoding))               {                   Reshtml = reader.ReadToEnd().Trim();                   if (!string.IsNullOrEmpty(Reshtml))                   {                       callback(true);                   }                   else                   {                       callback(false);                   }               }           }           catch (Exception)           {               callback(false);           }           finally { }                  }

 2.設定好修復代理設定

[DllImport(@"wininet",SetLastError = true,CharSet = CharSet.Auto,EntryPoint = "InternetSetOption",CallingConvention = CallingConvention.StdCall)]       public static extern bool InternetSetOption       (       int hInternet,       int dmOption,       IntPtr lpBuffer,       int dwBufferLength       );       /// <summary>       /// 設定代理       /// </summary>       /// <param name="ip_port">IP地址和連接埠</param>       public void SetProxy(string ip_port)       {           //開啟註冊表           RegistryKey regKey = Registry.CurrentUser;           string SubKeyPath = @"Software\Microsoft\Windows\CurrentVersion\Internet Settings";           RegistryKey optionKey = regKey.OpenSubKey(SubKeyPath, true);           //更改健值,設定代理,           optionKey.SetValue("ProxyEnable", 1);           if (ip_port.Length == 0)           {               optionKey.SetValue("ProxyEnable", 0);           }           optionKey.SetValue("ProxyServer", ip_port);           //啟用代理設定           InternetSetOption(0, 39, IntPtr.Zero, 0);           InternetSetOption(0, 37, IntPtr.Zero, 0);       }       /// <summary>       /// 不使用代理設定       /// </summary>       public void UnSetProxy()       {           RegistryKey regKey = Registry.CurrentUser;           string SubKeyPath = @"Software\Microsoft\Windows\CurrentVersion\Internet Settings";           RegistryKey optionKey = regKey.OpenSubKey(SubKeyPath, true);           //更改健值,設定代理,           optionKey.SetValue("ProxyEnable", 0);           //啟用代理設定           InternetSetOption(0, 39, IntPtr.Zero, 0);           InternetSetOption(0, 37, IntPtr.Zero, 0);       }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.