第一种方式:
/// <summary>
/// Http Post 可传文件参数
/// </summary>
/// <param name="url"></param>
/// <param name="dic">字符串 字典数据</param>
/// <param name="dicFile">文件 字典数据</param>
/// <returns></returns>
public string PostJsonDataFile(string url, Dictionary<string, string> dic, Dictionary<string, string> dicFile)
{
string str = "";
try
{
Uri uri = new Uri(url);
var httpclientHandler = new HttpClientHandler();
httpclientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
Logger.LoggerToFile($"{DateTime.Now} HttpPost:Api/RPA/GetHR 发起流程");
HttpClient client = new HttpClient(httpclientHandler);
var form = new MultipartFormDataContent();
string boundary = string.Format("--{0}", DateTime.Now.Ticks.ToString("x"));
form.Headers.Add("ContentType", $"multipart/form-data, boundary={boundary}");
if (dic != null)
{
foreach (var key in dic.Keys)
{
form.Add(new StringContent(dic[key].ToString()), key);
}
}
if (dicFile != null)
{
foreach (var key in dicFile.Keys)
{
var fileContent = new ByteArrayContent(File.ReadAllBytes(dicFile[key].ToString()));
fileContent.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
form.Add(fileContent, name: key, fileName: dicFile[key].ToString());
}
}
HttpResponseMessage response = client.PostAsync(url, form).Result;
str = response.Content.ReadAsStringAsync().Result;
Logger.LoggerToFile($"{DateTime.Now} HttpPost:Api/RPA/GetHR 結束流程{str}");
}
catch (Exception ex)
{
Logger.LoggerToFile($"{DateTime.Now} HttpPost:Api/RPA/GetHR 結束流程異常{ ex.ToString()}");
return ("PostJsonData:" + ex.ToString());
}
return str;
}
第一种调用方式:
XImageCollection images = document.Pages[1].Resources.Images;
if (images != null)
{
System.Drawing.Image qrImage = images[1].Grayscaled;
MemoryStream memoryStream = new MemoryStream();
qrImage.Save(memoryStream, ImageFormat.Jpeg);
byte[] buffer = memoryStream.GetBuffer();
//byte[] buffer = Convert.FromBase64String(fileBody);
//string str = Convert.ToBase64String(buffer);
try
{
using (FileStream fs = new FileStream(qrCodePath, FileMode.CreateNew))
{
fs.Write(buffer, 0, buffer.Length);
fs.Close();
}
HttpPostApi httpPostApi = new HttpPostApi();
Dictionary<string, string> param = new Dictionary<string, string>();
param.Add("imageFile", $"{qrCodePath}");
Logger.LoggerToFile($"{DateTime.Now} 识别二维码接口请求:【地址:{ConfigManager.QRCoderReaderUrl}】【参数:{JsonConvert.SerializeObject(param)}】");
var response = httpPostApi.PostJsonDataFile(ConfigManager.QRCoderReaderUrl, null, param);
Logger.LoggerToFile($"{DateTime.Now} 识别二维码接口结束:【{response}】");
JObject obj = JObject.Parse(response);
if (obj.GetValue("success").ToString() == "true")
{
return obj.GetValue("data").ToString();
}
else
{
return "";
}
//RestClient client = new RestClient(ConfigManager.QRCoderReaderUrl);
//client.Timeout = -1;
//var request = new RestRequest(Method.POST);
//request.AddFile("imageFile", qrCodePath);
//IRestResponse response = client.Execute(request);
//result = response.Content;
Logger.LoggerToFile(DateTime.Now + ": HttpGet:Api/Contract/ScanReadQRCode 识别二维码接口返回结果:" + response);
}
catch (Exception)
{
File.Delete(qrCodePath);
}
finally { File.Delete(qrCodePath); }
}
第二种方式:
/// <summary>
/// Http Post 可传文件参数
/// </summary>
/// <param name="url"></param>
/// <param name="dic">字符串 字典数据</param>
/// <param name="dicFile">文件 字典数据</param>
/// <returns></returns>
public string PostJsonDataFile(string url, Dictionary<string, string> dic, Dictionary<string, KeyValuePair<string,byte[]>> dicFile)
{
string str = "";
try
{
Uri uri = new Uri(url);
var httpclientHandler = new HttpClientHandler();
httpclientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
Logger.LoggerToFile($"{DateTime.Now} HttpPost:Api/RPA/GetHR 发起流程");
HttpClient client = new HttpClient(httpclientHandler);
var form = new MultipartFormDataContent();
string boundary = string.Format("--{0}", DateTime.Now.Ticks.ToString("x"));
form.Headers.Add("ContentType", $"multipart/form-data, boundary={boundary}");
if (dic != null) {
foreach (var key in dic.Keys)
{
form.Add(new StringContent(dic[key].ToString()), key);
}
}
if (dicFile != null)
{
foreach (var key in dicFile.Keys)
{
var fileContent = new ByteArrayContent(dicFile[key].Value);
//var fileContent = new ByteArrayContent(File.ReadAllBytes(dicFile[key].ToString()));
fileContent.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
form.Add(fileContent, name: key, fileName: dicFile[key].Key);
}
}
HttpResponseMessage response = client.PostAsync(url, form).Result;
str = response.Content.ReadAsStringAsync().Result;
Logger.LoggerToFile($"{DateTime.Now} HttpPost:Api/RPA/GetHR 結束流程{str}");
}
catch (Exception ex)
{
Logger.LoggerToFile($"{DateTime.Now} HttpPost:Api/RPA/GetHR 結束流程異常{ ex.ToString()}");
return ("PostJsonData:" + ex.ToString());
}
return str;
}
第二种调用方式:
System.Drawing.Image qrImage = images[1].Grayscaled;
MemoryStream memoryStream = new MemoryStream();
qrImage.Save(memoryStream, ImageFormat.Jpeg);
byte[] buffer = memoryStream.GetBuffer();
//byte[] buffer = Convert.FromBase64String(fileBody);
//string stream = Convert.ToBase64String(buffer);
try
{
HttpPostApi httpPostApi = new HttpPostApi();
Dictionary<string, KeyValuePair<string, byte[]>> param = new Dictionary<string, KeyValuePair<string, byte[]>>();
param.Add("imageFile", new KeyValuePair<string, byte[]>(Path.GetFileName(qrCodePath), buffer));
Logger.LoggerToFile($"{DateTime.Now} 识别二维码接口请求:【地址:{ConfigManager.QRCoderReaderUrl}】【参数:{JsonConvert.SerializeObject(param)}】");
var response = httpPostApi.PostJsonDataFile(ConfigManager.QRCoderReaderUrl,null, param);
Logger.LoggerToFile($"{DateTime.Now} 识别二维码接口结束:【{response}】");
JObject obj = JObject.Parse(response);
if (obj.GetValue("success").ToString() == "true")
{
return obj.GetValue("data").ToString();
}
else
{
return "";
}
Logger.LoggerToFile(DateTime.Now + ": HttpGet:Api/Contract/ScanReadQRCode 识别二维码接口返回结果:" + response);
}
catch (Exception)
{
File.Delete(qrCodePath);
}
finally { File.Delete(qrCodePath); }