最近尝试使用,在局域网内使用WebClient下载内容,碰到了生平第一次使用了代理的问题,于是乎查资料,于是乎有了此篇文章记录...
代码写于下面:
string getHost = "http://www.baidu.com";
WebClient wClient = new WebClient();
ebProxy p = null;W //代理类
string proxyAddressAndPort = ConfigurationManager.AppSettings["proxy"];
string proxyUserName = ConfigurationManager.AppSettings["proxyUserName"];
string proxyPwd = ConfigurationManager.AppSettings["proxyPwd"];
ICredentials cre; //身份验证信息类
cre = new NetworkCredential(proxyUserName, proxyPwd); //得到用户名和密码
, p = new WebProxy(proxyAddressAndPort, true, null, cre); //将信息放于代理类中(代理服务器URl,是否跳过代理,,身份验证信息)
WebRequest.DefaultWebProxy = p; //为Web请求设置代理
string retval = wClient.DownloadString(getHost);
Console.WriteLine(retval);
Console.WriteLine(retval.Replace(@"\t\n",""));
Console.WriteLine(retval.Replace(" ", ""));

浙公网安备 33010602011771号