zoukankan      html  css  js  c++  java
  • WebAPI学习笔记(4)Asp.net调用WebAPI Post方法获取附件

    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);
                        }
                    }
    }
  • 相关阅读:
    DM8168 新三板系统启动
    AndroidAndroid程序提示和消息button响应事件
    Linux核心regulator建筑和准备
    对话(VC_Win32)
    VOJ 1067 Warcraft III 守望者的烦恼 (矩阵高速功率+dp)
    有所述网络流的上限和下限
    Matlab Newton‘s method
    Cocos2d-Java安装和配置跨平台游戏引擎以及相关的开发工具
    n每个计数的概率和发生骰子--动态规划
    自己写CPU第九阶段(3)——加载存储指令说明2(swl、swr)
  • 原文地址:https://www.cnblogs.com/61007257Steven/p/11880739.html
Copyright © 2011-2022 走看看