WebAPI学习笔记(4)Asp.net调用WebAPI Post方法获取附件
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了WebAPI学习笔记(4)Asp.net调用WebAPI Post方法获取附件,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2098字,纯文字阅读大概需要3分钟。
内容图文
1、WebAPI方法:
public HttpResponseMessage GetAttachmentList() { HttpResponseMessage result = null; try {string fileName1 = "Test001.txt"; string fileName2 = "Test002.pptx"; string fileName3 = "Test003.zip"; string TempFileSavePath = ConfigurationHelper.GetDownloadFileDefaultSavePath(); byte[] bytes1 = MethodHelper.FileToByte(HttpContext.Current.Server.MapPath(TempFileSavePath + fileName1)); byte[] bytes2 = MethodHelper.FileToByte(HttpContext.Current.Server.MapPath(TempFileSavePath + fileName2)); byte[] bytes3 = MethodHelper.FileToByte(HttpContext.Current.Server.MapPath(TempFileSavePath + fileName3)); List<AttachmentModel> attachmentList = new List<AttachmentModel>(); attachmentList.Add(new AttachmentModel(fileName1, bytes1)); attachmentList.Add(new AttachmentModel(fileName2, bytes2)); attachmentList.Add(new AttachmentModel(fileName3, bytes3)); result = MethodHelper.GetHttpResponseMessage(ConvertJson.List2Json<AttachmentModel>(attachmentList)); } catch (Exception ex) { } return result; }
public static HttpResponseMessage GetHttpResponseMessage(string content) { return new HttpResponseMessage { Content = new StringContent(content, Encoding.GetEncoding("UTF-8"), "application/json") }; }
2、调用方式:
//测试附件 string Username = "xxx"; string Password = "xxx"; using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes($"{Username}:{Password}"))); HttpContent httpContent = new StringContent("", Encoding.UTF8); httpContent.Headers.Add("user-key", "xxx"); httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); Uri address = new Uri("https://localhost:44300/api/issues/GetAttachmentList"); var response = client.PostAsync(address, httpContent).Result.Content.ReadAsStringAsync().Result;//返回值 List<AttachmentModel> list = JsonConvert.DeserializeObject<List<AttachmentModel>>(response); if (list != null) { for (int i = 0; i < list.Count; i++) { string filePath = HttpContext.Current.Server.MapPath("Temp/" + list[i].fileName); ByteToFile(list[i].bytes, filePath); } } }
内容总结
以上是互联网集市为您收集整理的WebAPI学习笔记(4)Asp.net调用WebAPI Post方法获取附件全部内容,希望文章能够帮你解决WebAPI学习笔记(4)Asp.net调用WebAPI Post方法获取附件所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。