[.NET] 使用HttpClient操作HFS (HTTP File Server)
前言
本篇文章介绍如何使用HttpClient操作HFS (HTTP File Server),为自己留个纪录也希望能帮助到有需要的开发人员。关于HTTP File Server的介绍、安装、设定,可以参考下列参考资料:
功能
AddFile
-
Execute
// Variables string url = @"http://localhost:8080/HFS/"; string fileName = @"AAA.jpg"; string filePath = @"C:\Users\clark\Desktop\AAA.jpg"; // Execute this.AddFile(url, fileName, File.OpenRead(filePath));
-
Function
public void AddFile(string url, string fileName, Stream fileStream) { #region Contracts if (string.IsNullOrEmpty(url) == true) throw new ArgumentException(); if (string.IsNullOrEmpty(fileName) == true) throw new ArgumentException(); if (fileStream == null) throw new ArgumentException(); #endregion // FileStream using (fileStream) { // HttpClient using (var httpClient = new HttpClient()) { // Boundary string boundaryString = "----------------------------" + DateTime.Now.Ticks.ToString("x"); byte[] boundaryBytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundaryString + "\r\n"); // Header string headerString = string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n Content-Type: application/octet-stream\r\n\r\n", "file", fileName); byte[] headerBytes = System.Text.Encoding.UTF8.GetBytes(headerString); // RequestContent using (var memoryStream = new MemoryStream()) using (var requestContent = new StreamContent(memoryStream)) { // Headers requestContent.Headers.Add("Content-Type", "multipart/form-data; boundary=" + boundaryString); // Content memoryStream.Write(boundaryBytes, 0, boundaryBytes.Length); memoryStream.Write(headerBytes, 0, headerBytes.Length); fileStream.CopyTo(memoryStream); memoryStream.Position = 0; // Post using (var response = httpClient.PostAsync(url, requestContent).Result) { // Response if (response == null) throw new InvalidOperationException(); if (response.IsSuccessStatusCode == false) throw new InvalidOperationException(); } } } } }
RemoveFile
-
Execute
// Variables string url = @"http://localhost:8080/HFS/"; string fileName = @"AAA.jpg"; // Execute this.RemoveFile(url, fileName);
-
Function
public void RemoveFile(string url, string fileName) { #region Contracts if (string.IsNullOrEmpty(url) == true) throw new ArgumentException(); if (string.IsNullOrEmpty(fileName) == true) throw new ArgumentException(); #endregion // HttpClient using (var httpClient = new HttpClient()) { // RequestContent using (var requestContent = new FormUrlEncodedContent(new[] { // Content new KeyValuePair<string, string>("action", "delete"), new KeyValuePair<string, string>("selection", fileName) })) { // Post using (var response = httpClient.PostAsync(url, requestContent).Result) { // Response if (response == null) throw new InvalidOperationException(); if (response.IsSuccessStatusCode == false) throw new InvalidOperationException(); } } } }
期許自己~
能以更簡潔的文字與程式碼,傳達出程式設計背後的精神。
真正做到「以形寫神」的境界。
分类:
.NET
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
2015-03-04 [ASP.NET MVC] 使用Bootsnipp样式