重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
本篇文章为大家展示了使用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怎么下载图片至本地,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。