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

        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM