日期:2014-05-20 浏览次数:20998 次
WebClient mywebclient = new WebClient(); byte[] bytes = mywebclient.DownloadData("http://seasoncoffee.cn.alibaba.com/athena/contact/seasoncoffee.html");
/// <summary> /// 访问网络上的文件HTML /// </summary> /// <param name="url"></param> /// <returns></returns> public static byte[] webRequestByte(string url) { WebRequest request = WebRequest.Create(url); WebResponse response = request.GetResponse(); Stream resStream = response.GetResponseStream(); int bytelength=(int)response.ContentLength; Byte[] bytevalue = new Byte[bytelength]; // resStream.Read(bytevalue, 0, bytevalue.Length); int num=0; int m; while (1 > 0) { m = resStream.Read(bytevalue, num, bytelength); if(m==0)break; num += m; //当前已下载的字节数 bytelength -= m; //剩余字节数 总字节数:(int)response.ContentLength } resStream.Close(); return bytevalue; // StreamReader sr = new StreamReader(resStream, System.Text.Encoding.Default); //转换成字符串 输出 // ContentHtml.Text = sr.ReadToEnd(); // sr.Close(); //WebClient 得到字节流 //WebClient wc=new WebClient(); // Stream srm=wc.OpenRead(TempDown); //区别在于 WebRequest 是 system.net 空间下的 抽象类,你可以对它进行扩展 //WebClient 非抽象类 }