大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
本篇文章为大家展示了使用HttpWebRequest怎么下载图片至本地,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名申请、雅安服务器托管、营销软件、网站建设、隆回网站维护、网站推广。HttpWebRequest发送web请求,获取流文件,保存至本地
using System; using System.IO; using System.Net; using System.Text; using System.Web.Mvc; namespace Web.Controllers { public class HomeController : Controller { string url = "http://www.***.com/Image.aspx?ucode=ucode"; public Stream GetFileToStream(string filePath) { FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); byte[] bytes = new byte[fileStream.Length]; fileStream.Read(bytes, 0, bytes.Length); fileStream.Close(); Stream streamFile = new MemoryStream(bytes); return streamFile; } public void Method1() { string fileName = @"\upload\" + GenerateTimeStamp() + ".jpg"; string webPath = Server.MapPath(fileName); string pathName = @"d:\" + GenerateTimeStamp() + ".jpg"; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.ServicePoint.Expect100Continue = false; req.Method = "GET"; req.KeepAlive = true; req.ContentType = "image/png"; HttpWebResponse rsp = (HttpWebResponse)req.GetResponse(); System.IO.Stream stream = null; try { //以字符流的方式读取HTTP响应 stream = rsp.GetResponseStream(); System.Drawing.Image.FromStream(stream).Save(pathName); } finally { // 释放资源 if (stream != null) stream.Close(); if (rsp != null) rsp.Close(); } } protected void Method2() { string fileName = @"\upload\" + GenerateTimeStamp() + ".jpg"; string webPath = Server.MapPath(fileName); string pathName = @"d:\" + GenerateTimeStamp() + ".jpg"; HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url); myRequest.Method = "GET"; HttpWebResponse myResponse = null; try { myResponse = (HttpWebResponse)myRequest.GetResponse(); StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); Stream stream = myResponse.GetResponseStream(); #region 保存下载图片 MemoryStream ms = null; Byte[] buffer = new Byte[myResponse.ContentLength]; int offset = 0, actuallyRead = 0; do { actuallyRead = stream.Read(buffer, offset, buffer.Length - offset); offset += actuallyRead; } while (actuallyRead > 0); ms = new MemoryStream(buffer); byte[] buffurPic = ms.ToArray(); System.IO.File.WriteAllBytes(pathName, buffurPic); #endregion } //异常请求 catch (WebException ex) { } } protected void Method3() { string fileName = @"\upload\" + GenerateTimeStamp() + ".jpg"; string webPath = Server.MapPath(fileName); string pathName = @"d:\" + GenerateTimeStamp() + ".jpg"; HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url); myRequest.Method = "GET"; HttpWebResponse myResponse = null; try { myResponse = (HttpWebResponse)myRequest.GetResponse(); StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); Stream stream = myResponse.GetResponseStream(); #region 保存下载图片 FileStream fileStream = new FileStream(pathName, FileMode.Create, FileAccess.Write); byte[] bytes = new byte[1024]; int readSize = 0; while ((readSize = stream.Read(bytes, 0, 1024)) > 0) { fileStream.Write(bytes, 0, readSize); fileStream.Flush(); } #endregion myResponse.Close(); stream.Close(); fileStream.Close(); } //异常请求 catch (WebException ex) { } finally { } } public string GenerateTimeStamp() { TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0); return Convert.ToInt64(ts.TotalMilliseconds).ToString(); } } }
上述内容就是使用HttpWebRequest怎么下载图片至本地,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联成都网站设计公司行业资讯频道。
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。