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 "";
            }

        }

 

转载于:https://www.cnblogs.com/yxzs/p/10795068.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值