C#实现HTTP下载文件的方法
作者:shichen2014 时间:2022-01-19 02:35:50
本文实例讲述了C#实现HTTP下载文件的方法。分享给大家供大家参考。
主要实现代码如下:
/// <summary>
/// Http下载文件
/// </summary>
public static string HttpDownloadFile(string url, string path)
{
// 设置参数
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
//发送请求并获取相应回应数据
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
Stream responseStream = response.GetResponseStream();
//创建本地文件写入流
Stream stream = new FileStream(path, FileMode.Create);
byte[] bArr = new byte[1024];
int size = responseStream.Read(bArr, 0, (int)bArr.Length);
while (size > 0)
{
stream.Write(bArr, 0, size);
size = responseStream.Read(bArr, 0, (int)bArr.Length);
}
stream.Close();
responseStream.Close();
return path;
}
希望本文所述对大家的C#程序设计有所帮助。
标签:C#,HTTP,下载,文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
springBoot集成Elasticsearch 报错 Health check failed的解决
2022-12-07 05:18:16
![](https://img.aspxhome.com/file/2023/2/69872_0s.jpg)
Spring Boot启动时调用自己的非web逻辑
2022-02-15 11:40:10
maven资源过滤打包后文件变大的处理方法
2023-10-05 21:38:24
![](https://img.aspxhome.com/file/2023/0/61670_0s.png)
Java判断字符串是否是整数或者浮点数的方法
2022-04-30 10:06:20
Android使用GridView实现日历的简单功能
2023-10-07 07:14:31
![](https://img.aspxhome.com/file/2023/5/90125_0s.jpg)
C#实现启动项管理的示例代码
2022-07-30 09:09:04
![](https://img.aspxhome.com/file/2023/2/87052_0s.png)
C# 基于TCP 实现扫描指定ip端口的方式示例
2021-11-28 07:55:24
![](https://img.aspxhome.com/file/2023/3/96293_0s.png)
Java调用groovy实现原理代码实例
2023-05-16 16:41:51
![](https://img.aspxhome.com/file/2023/3/68543_0s.jpg)
Java微信公众平台开发(15) 微信JSSDK的使用
2023-06-16 19:58:39
![](https://img.aspxhome.com/file/2023/8/78048_0s.jpg)
SpringBoot 返回Json实体类属性大小写的解决
2023-08-05 12:30:49
经典实例讲解C#递归算法
2022-04-11 22:34:16
![](https://img.aspxhome.com/file/2023/2/94552_0s.gif)
C#操作FTP出现500错误解决办法
2022-06-05 16:20:26
![](https://img.aspxhome.com/file/2023/3/94593_0s.png)
Spring Security验证流程剖析及自定义验证方法
2023-10-01 22:05:58
![](https://img.aspxhome.com/file/2023/3/92643_0s.png)
Java开发环境配置方法
2022-05-25 02:02:29
![](https://img.aspxhome.com/file/2023/0/69340_0s.jpg)
Flutter自定义圆盘取色器
2023-07-05 23:55:43
![](https://img.aspxhome.com/file/2023/0/96010_0s.gif)
java图片添加水印实例代码分享
2022-06-10 04:06:03
C#线程入门教程之单线程介绍
2022-03-15 20:37:28
![](https://img.aspxhome.com/file/2023/9/69819_0s.jpg)
Java实现颜色渐变效果
2023-08-25 11:10:45
![](https://img.aspxhome.com/file/2023/2/58062_0s.png)
C#中char和string的入门使用教程
2022-06-14 15:07:17
![](https://img.aspxhome.com/file/2023/5/96305_0s.png)
C#实现简单的计算器小功能
2023-05-13 06:46:11
![](https://img.aspxhome.com/file/2023/1/77301_0s.jpg)