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();
}