服务器之家

服务器之家 > 正文

HttpWebRequest实现下载图片至本地

时间:2022-02-27 15:36     来源/作者:C#教程网

本文为大家分享了HttpWebRequest实现下载图片至本地的具体代码,供大家参考,具体内容如下

HttpWebRequest发送web请求,获取流文件,保存至本地

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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();
    }
  }
}

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

相关文章

热门资讯

2022年最旺的微信头像大全 微信头像2022年最新版图片
2022年最旺的微信头像大全 微信头像2022年最新版图片 2022-01-10
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整 2021-08-24
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
返回顶部