zoukankan      html  css  js  c++  java
  • RestClient(接口请求)

      一、电子签章:通过接口,传入参数中有pdf文件,和其他参数,在文件上盖上电子签章。

      引入:通过NuGet安装RestSharp,注意版本。

         备注:如果后面程序运行有错,可以看是引用是否更改了web.config文件(一般会更改Newtonsoft.Json的版本)

      代码:

    /// <summary>
            /// 电子签章(盖章)
            /// </summary>
            /// <param name="apiUrl"></param>
            /// <param name="filePath"></param>
            /// <param name="type">为1 表示报名确认函   为2 表示邀约人员名单</param>
            /// <returns></returns>
            public static string SendCaESignature(string apiUrl, string filePath, string type)
            {
                try
                {
                    #region 初始化参数
                    string url = ConfigurationManager.AppSettings["caESignature"].ToString() + apiUrl;
    
                    string picName = "***.gif";
                    string certName = "***.pfx";
                    string page = "0";
                    string posX = "";
                    string posY = "";
    
                    // 确认涵 x、y轴
                    string hanPosX = ConfigurationManager.AppSettings["hanPosX"].ToString();
                    string hanPosY = ConfigurationManager.AppSettings["hanPosY"].ToString();
    
                    // 邀约人员名单
                    string joinPosX = ConfigurationManager.AppSettings["joinPosX"].ToString();
                    string joinPosY = ConfigurationManager.AppSettings["joinPosY"].ToString();
    
                    // 不同名单,签章位置不一样
                    if (type == "1")
                    {
                        posX = hanPosX;
                        posY = hanPosY;
                    }
                    else
                    {
                        posX = joinPosX;
                        posY = joinPosY;
                    }
                    #endregion
    
                    #region 将文件转成二进制
                    byte[] fileContentByte = new byte[10240]; // 文件内容二进制
                    FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                    fileContentByte = new byte[fs.Length]; // 二进制文件
                    fs.Read(fileContentByte, 0, Convert.ToInt32(fs.Length));
                    fs.Close();
    
                    #endregion
    
                    string postUrl = ConfigurationManager.AppSettings["caESignature"].ToString();
                    var restClient = new RestClient(postUrl);
                    RestRequest re = new RestRequest(apiUrl, Method.POST);
                    re.AddQueryParameter("picName", picName);
                    re.AddQueryParameter("certName", certName);
                    re.AddQueryParameter("page", page);
                    re.AddQueryParameter("posX", posX);
                    re.AddQueryParameter("posY", posY);
                    re.AddFileBytes("pdfFile", fileContentByte, "报名确认涵", "application/octet-stream");
    
                    return restClient.Execute(re).Content;
                }
                catch (Exception e)
                {
                    FileHelper.Log("电子签章:" + e.Message);
                    return "";
                }
    
            }

    二、下载电子签章:通过接口下载二进制文件流

    代码:

    /// <summary>
            /// 下载电子签章
            /// </summary>
            /// <param name="fileIden"></param>
            /// <returns></returns>
            public static string DownFileCA(string fileIden)
            {
                try
                {
                    //创建本地缓存文件夹
                    string basePath = System.Web.HttpContext.Current.Server.MapPath("~/Temp/");
                    if (!Directory.Exists(basePath))
                    {
                        Directory.CreateDirectory(basePath);
                    }
                    string filePath = basePath + string.Format("PDF_{0}.pdf", Guid.NewGuid().ToString("N"));
    
                    string returnPath = "";
                    if (File.Exists(filePath))
                    {//如果已经存在,那么就不需要拷贝了,如果没有,那么就进行拷贝
                        return returnPath;
                    }
                    else
                    {
                        FileStream fs = File.Create(filePath);
                        fs.Close();
                    }
    
                    string postUrl = ConfigurationManager.AppSettings["caESignature"].ToString();
                    var restClient = new RestClient(postUrl);
                    RestRequest re = new RestRequest("/Signature/download", Method.POST);
                    re.AddQueryParameter("fileIden", fileIden);
                    
    
                    byte[] byteArray = restClient.DownloadData(re);
    
                    using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write))
                    {
                        fs.Write(byteArray, 0, byteArray.Length);
                        fs.Close();
                    }
    
                    return filePath;
                }
                catch (Exception ex)
                {
                    FileHelper.Log("电子签章:" + ex.Message);
    
                    return "";
                }
    
            }
  • 相关阅读:
    Git使用方法
    设计模式之原型模式
    php基础查找算法
    php基础排序算法
    设计模式之工厂模式
    设计模式之代理模式
    设计模式之装饰者模式
    设计模式之六大原则
    设计模式之策略模式
    设计模式之简单工厂模式
  • 原文地址:https://www.cnblogs.com/yxzs/p/10795068.html
Copyright © 2011-2022 走看看