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