WebClient設置代理


try
{
  

WC = new System.Net.WebClient();
//實例化代理服務器對象
WebProxy myProxy = new WebProxy(@"http://192.168.0.1:8080",true);
// 設置用戶帳號username,password
myProxy.Credentials = new NetworkCredential("username", "password");
// 為WebClient設置proxy
WC.Proxy = myProxy;
s = WC.OpenRead(url);
sr = new System.IO.StreamReader(s);
CodeSource = sr.ReadToEnd();
sr.Close();
sr.Dispose();
s.Close();
s.Dispose();

}
catch (Exception ex)
{
if (ex.Message.ToString().Contains("401"))
MessageBox.Show("Web Access denied (Unauthorized)");
else
MessageBox.Show(ex.Message);
}
finally
{
WC.Dispose();
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM