RestClient(インタフェースリクエスト)

13590 ワード

一、電子署名:インタフェースを通じて、入力パラメータの中にpdfファイルとその他のパラメータがあり、ファイルに電子署名を押す.
導入:NuGetでRestSharpをインストールし、バージョンに注意します.
備考:後でプログラムが間違っている場合は、web.configファイルが変更されたかどうかを参照することができます(一般的にはNewtonsoft.Jsonのバージョンが変更されます)
 
コード:/// /// ( ) /// /// /// /// 1 2 /// 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 ""; } }
二、電子署名のダウンロード:インタフェースを通じてバイナリファイルストリームをダウンロードする
コード:
  /// /// /// /// /// 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