C# 应用 - 使用 HttpClient 发起上传文件、下载文件请求
1. 示例代码
using System;
using System.IO;
using System.Net.Http;
/// <summary>
/// 下载文件
/// </summary>
/// <param name="serverFileName">服务器上文件名 如 close.png</param>
/// <param name="localFileName">要保存到本地的路径全名 如:C://Download/close.png</param>
/// <returns></returns>
public static bool DownLoad(string uri, string localFileName)
{
var server = new Uri(uri);
var p = Path.GetDirectoryName(localFileName);
if (!Directory.Exists(p)) Directory.CreateDirectory(p);
// 发起请求并异步等待结果
var httpClient = new HttpClient();
var responseMessage = httpClient.GetAsync(server).Result;
if (responseMessage.IsSuccessStatusCode)
{
using (var fs = File.Create(localFileName))
{
// 获取结果,并转成 stream 保存到本地。
var streamFromService = responseMessage.Content.ReadAsStreamAsync().Result;
streamFromService.CopyTo(fs);
return true;
}
}
else
return false;
}
/// <summary>
/// 通过webapi上传单张图片文件
/// </summary>
/// <param name="uploadUri">上传路径</param>
/// <param name="FullFileNames">图片文件的字节编码</param>
/// <param name="FileStars">上传文件的保存路径前缀,可null,null则不分文件夹保存</param>
/// <param name="FileNames">上传文件的保存名称</param>
/// <returns></returns>
public static List<UploadFile> UploadImageFiles(string uploadUri, byte[] FullFile, string FileStars, string FileNames)
{
// 判断传入参数是否正确
if (string.IsNullOrEmpty(uploadUri) || FullFile == null || FileNames == null)
{
Common.WriteLog.Error("上传文件时传入参数异常");
return null;
}
// 上传服务uri
var clineturi = new Uri(uploadUri);
using (var client = new HttpClient())
{
client.Timeout = new TimeSpan(0, 0, 0, 0, Common.RESTTimeOut);
// 以MultipartFormData格式上传
using (var content = new MultipartFormDataContent())
{
// 文件往content添加一条ByteArrayContent
// 发送二进制内容
var fileContent = new ByteArrayContent(FullFile);
// 设置上传后保存的前缀路径和文件名称(文件名称如果重复会特殊处理)
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
if (FileStars != null) fileContent.Headers.ContentDisposition.FileNameStar = FileStars;
fileContent.Headers.ContentDisposition.FileName = FileNames;
content.Add(fileContent);
try
{
// 上传文件,获取返回的字符串内容
var result = client.PostAsync(clineturi, content).Result.Content.ReadAsStringAsync().Result;
return Newtonsoft.Json.JsonConvert.DeserializeObject<List<UploadFile>>(result);
}
catch (Exception ex)
{
Common.WriteLog.Error(string.Format("上传文件出错!uri={0},FullFileNames={1}", uploadUri, FileStars.ToString()), ex);
return null;
}
}
}
}
2. Http 系列
2.1 发起请求
使用 HttpWebRequest 发起 Http 请求:https://www.cnblogs.com/MichaelLoveSna/p/14501036.html
使用 WebClient 发起 Http 请求 :https://www.cnblogs.com/MichaelLoveSna/p/14501582.html
使用 HttpClient 发起 Http 请求:https://www.cnblogs.com/MichaelLoveSna/p/14501592.html
使用 HttpClient 发起上传文件、下载文件请求:https://www.cnblogs.com/MichaelLoveSna/p/14501603.html
2.2 接受请求
使用 HttpListener 接受 Http 请求:https://www.cnblogs.com/MichaelLoveSna/p/14501628.html
使用 WepApp 接受 Http 请求:https://www.cnblogs.com/MichaelLoveSna/p/14501612.html
使用 WepApp 处理文件上传、下载请求:https://www.cnblogs.com/MichaelLoveSna/p/14501616.html
C# 应用 - 使用 HttpClient 发起上传文件、下载文件请求的更多相关文章
- 利用SecureCRT上传、下载文件(使用sz与rz命令),超实用!
利用SecureCRT上传.下载文件(使用sz与rz命令),超实用! 文章来源:http://blog.csdn.net/dongqinliuzi/article/details/39623169 借 ...
- Linux--用SecureCRT来上传和下载文件
SecureCRT下的文件传输协议有以下几种:ASCII.Xmodem.Ymodem.Zmodem ASCII:这是最快的传输协议,但只能传送文本文件. Xmodem:这种古老的传输协议速度较慢,但由 ...
- SecureCRT来上传和下载文件
引用:https://www.cnblogs.com/zhengyihan1216/p/6260667.html Linux--用SecureCRT来上传和下载文件 SecureCRT下的文件传输协议 ...
- 解惑:如何使用SecureCRT上传和下载文件、SecureFX乱码问题
解惑:如何使用SecureCRT上传和下载文件.SecureFX乱码问题 一.前言 很多时候在windows平台上访问Linux系统的比较好用的工具之一就是SecureCRT了,下面介绍一下这个软件的 ...
- SecureCRT上传和下载文件
SecureCRT上传和下载文件(下载默认目录) SecureCR 下的文件传输协议有ASCII .Xmodem .Ymodem .Zmodem ASCII:这是最快的传输协议,但只能传送文本文件. ...
- 11、只允许在主目录下上传和下载文件,不允许用putty登录
创建用户xiao, 使其只允许在用户主目录 (/var/www/html)下上传和下载文件,不允许用putty登录 (为了安全起见,不给过多的权限) 1.创建xiao用户 [root@localh ...
- 每天一个linux命令(26):用SecureCRT来上传和下载文件
用SSH管理linux服务器时经常需要远程与本地之间交互文件.而直接用SecureCRT自带的上传下载功能无疑是最方便的,SecureCRT下的文件传输协议有ASCII.Xmodem.Zmodem. ...
- secureCRT简单上传、下载文件记录
secureCRT简单上传.下载文件记录: 1)sz下载 -y 覆盖 2)rz上传 -y 覆盖 3)以上两个命令属于安装时包含在“Dial-up Networking Support"组中 ...
- 每天一个linux命令(26)--用SecureCRT来上传和下载文件
用SSH管理Linux 服务器时经常需要远程与本地之间交互文件,而直接使用 SecureCRT 自带的上传下载功能无疑是最方便的,SecureCRT下的文件传输协议有ASCII.Xmodem.Zmod ...
随机推荐
- 僵尸进程 & 孤儿进程
参考博文 基本概念 僵尸进程:是所有进程都会进入的一种进程状态,子进程退出,而父进程并没有调用 wait() 或 waitpid() 获取子进程的状态信息,那么子进程的 PID 和 进程描述符 等资源 ...
- codeforce 855B
B. Marvolo Gaunt's Ring time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- μC/OS-III---I笔记12---任务管理
任务管理任务切换应该算是UCOS最基本的部分,首先保存当前任务寄存器的内容到当前任务的堆栈:接着弹出即将进行的任务的堆栈内容到寄存器中然后就是按寄存器内容执行,这个过程成为上下文切换.任务堆栈在创建任 ...
- SEO & JSON-LD & structured-data
SEO & JSON-LD & structured-data script type="application/ld+json" script type=&quo ...
- WebView & iframe
WebView & iframe https://developer.android.com/reference/android/webkit/WebView.html Web-based c ...
- React SSR in Action
React SSR in Action react render HTML string from the server ReactDOMServer https://reactjs.org/docs ...
- HTML5 Server-Sent Events
HTML5 Server-Sent Events SSE demo https://www.w3schools.com/html/tryit.asp?filename=tryhtml5_sse htt ...
- 11月16日NGK公链第13期官方快讯!
- 画一个PBN大角度飞越转弯保护区
今天出太阳了,尽管街上的行人依旧很少,但心情开始不那么沉闷了.朋友圈里除了关注疫情的最新变化之外,很多人已经开始选择读书或是和家人一起渡过这个最漫长的春节假期.陕西广电网络春节期间所有点播节目一律 ...
- Git 学习相关笔记
Git Bash 相关命令学 基础概念 参考: https://www.cnblogs.com/gaoht/p/9087070.html https://www.runoob.com/git/git- ...