日期:2014-05-18 浏览次数:21152 次
public static string GetUrl2Data(String url, bool stat) { WebRequest myWebRequest = null; WebResponse myWebResponse = null; Stream streamResponse = null; StreamReader streamRead = null; StringBuilder sbuilder = null; try { myWebRequest = WebRequest.Create(url); WebProxy myProxy = new WebProxy(); if (stat == true)//使用代理 { //代理地址 string proxyAddressPort = Utils.ReadIniData("run", "ProxyAddressPort", "", ROOT + "\\netbench.ini"); string username = Utils.ReadIniData("run", "ProxyUserName", "", ROOT + "\\netbench.ini"); string password = Utils.ReadIniData("run", "ProxyPassWord", "", ROOT + "\\netbench.ini"); Uri newUri = new Uri(proxyAddressPort); myProxy.Address = newUri; myProxy.Credentials = new NetworkCredential(username, password); myWebRequest.Proxy = myProxy; } else { myWebRequest.Proxy = myProxy; } myWebResponse = myWebRequest.GetResponse(); streamResponse = myWebResponse.GetResponseStream(); streamRead = new StreamReader(streamResponse); Char[] readBuff = new Char[256]; int count = streamRead.Read(readBuff, 0, 256); sbuilder = new StringBuilder(); while (count > 0) { sbuilder.Append(new String(readBuff, 0, count)); count = streamRead.Read(readBuff, 0, 256); } } catch (UriFormatException e) { Console.WriteLine("\nUriFormatException is thrown.Message is {0}", e.Message); Console.WriteLine("\nThe format of the myProxy address you entered is invalid"); } finally { if (streamResponse != null) streamResponse.Close(); if (streamRead != null) streamRead.Close(); if (myWebResponse != null) myWebResponse.Close(); } if (sbuilder != null && !("").Equals(sbuilder.ToString())) return sbuilder.ToString(); else return ""; }