欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

HttpWebRequest实现下载图片至本地

程序员文章站 2023-12-17 15:02:40
本文为大家分享了httpwebrequest实现下载图片至本地的具体代码,供大家参考,具体内容如下 httpwebrequest发送web请求,获取流文件,保存至本地...

本文为大家分享了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();
    }
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: