HttpWebRequest实现下载图片至本地
来源:本站原创|时间:2020-01-10|栏目:C#教程|点击: 次
本文为大家分享了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(); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
您可能感兴趣的文章
- 01-10C#实现txt定位指定行完整实例
- 01-10WinForm实现仿视频播放器左下角滚动新闻效果的方法
- 01-10C#实现清空回收站的方法
- 01-10C#实现读取注册表监控当前操作系统已安装软件变化的方法
- 01-10C#实现多线程下载文件的方法
- 01-10C#实现Winform中打开网页页面的方法
- 01-10C#实现远程关闭计算机或重启计算机的方法
- 01-10C#自定义签名章实现方法
- 01-10C#文件断点续传实现方法
- 01-10winform实现创建最前端窗体的方法
阅读排行
本栏相关
- 01-10C#通过反射获取当前工程中所有窗体并
- 01-10关于ASP网页无法打开的解决方案
- 01-10WinForm限制窗体不能移到屏幕外的方法
- 01-10WinForm绘制圆角的方法
- 01-10C#实现txt定位指定行完整实例
- 01-10WinForm实现仿视频播放器左下角滚动新
- 01-10C#停止线程的方法
- 01-10C#实现清空回收站的方法
- 01-10C#通过重写Panel改变边框颜色与宽度的
- 01-10C#实现读取注册表监控当前操作系统已
随机阅读
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 01-11ajax实现页面的局部加载
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 01-10delphi制作wav文件的方法
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 01-10SublimeText编译C开发环境设置
- 01-10C#中split用法实例总结
- 04-02jquery与jsp,用jquery