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 = "";
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 == "")
{
posX = hanPosX;
posY = hanPosY;
}
else
{
posX = joinPosX;
posY = joinPosY;
}
#endregion #region 将文件转成二进制
byte[] fileContentByte = new byte[]; // 文件内容二进制
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
fileContentByte = new byte[fs.Length]; // 二进制文件
fs.Read(fileContentByte, , 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, , byteArray.Length);
fs.Close();
} return filePath;
}
catch (Exception ex)
{
FileHelper.Log("电子签章:" + ex.Message); return "";
} }
上一篇:解决mybatis generator警告Cannot obtain primary key information from the database, generated objects may be incomplete


下一篇:java实现点选汉字验证码