|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Net.Http;
|
|
|
|
using System.Net.Http.Headers;
|
|
|
|
using System.Text;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using CPF.Controls;
|
|
|
|
using AKSWebBrowser.Commen;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Net.NetworkInformation;
|
|
|
|
using AKS.EnterpriseLibrary.WebBrowser;
|
|
|
|
using System.Globalization;
|
|
|
|
|
|
|
|
namespace AksWebBrowser.Common
|
|
|
|
{
|
|
|
|
public class ChunkedUpload
|
|
|
|
{
|
|
|
|
private readonly HttpClient _httpClient;
|
|
|
|
public static string callback = string.Empty;
|
|
|
|
public ChunkedUpload(HttpClient httpClient)
|
|
|
|
{
|
|
|
|
_httpClient = httpClient;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<string> UploadFileAsync(string url, string filePath)
|
|
|
|
{
|
|
|
|
string ret = string.Empty;
|
|
|
|
using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
|
|
|
|
{
|
|
|
|
FileInfo fileInfo = new FileInfo(filePath);
|
|
|
|
int totalParts = 1;
|
|
|
|
int chunkNumber = 1;
|
|
|
|
// 读取文件流其实位置
|
|
|
|
var fileStreamPos = 0;
|
|
|
|
var uploadUrl = $"{url}?partNumber={chunkNumber}&chunks={totalParts}&size={fileInfo.Length}&start={fileStreamPos}&end={fileInfo.Length}&total={fileInfo.Length}&FileName={Path.GetFileName(filePath)}";
|
|
|
|
|
|
|
|
using (var client = new HttpClient())
|
|
|
|
{
|
|
|
|
|
|
|
|
var formData = new MultipartFormDataContent();
|
|
|
|
formData.Add(new StreamContent(fileStream, (int)fileStream.Length), "file", Path.GetFileName(filePath) + ".partNumber-1");
|
|
|
|
var response = await client.PostAsync(uploadUrl, formData);
|
|
|
|
var responseString = await response.Content.ReadAsStringAsync();
|
|
|
|
fileStream.Close();
|
|
|
|
fileStream.Dispose();
|
|
|
|
ret = responseString;
|
|
|
|
JObject jo = (JObject)JsonConvert.DeserializeObject(ret);
|
|
|
|
Log.Error("上传文件返回:" + ret);
|
|
|
|
if (Convert.ToBoolean(jo["IsSucceed"].ToString()) == true)
|
|
|
|
{
|
|
|
|
string result = jo["result"].ToString();
|
|
|
|
JObject jo1 = (JObject)JsonConvert.DeserializeObject(result);
|
|
|
|
ret = jo1["url"].ToString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// get请求 Task<string>
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
|
|
public async Task<string> getReq(string query)
|
|
|
|
{
|
|
|
|
string ret = string.Empty;
|
|
|
|
using (var httpClient = new HttpClient())
|
|
|
|
{
|
|
|
|
// 构建带参数的请求URI
|
|
|
|
var uri = Parame.gpyUrl + query;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// 发送GET请求
|
|
|
|
HttpResponseMessage response = await httpClient.GetAsync(uri);
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
if (response.StatusCode.ToString() == "OK")
|
|
|
|
{
|
|
|
|
// 读取响应内容
|
|
|
|
string body = await response.Content.ReadAsStringAsync();
|
|
|
|
ret = body;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (HttpRequestException e)
|
|
|
|
{
|
|
|
|
//Log.Error(" get请求: " + e.Message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 获取签字版数据
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
public async Task<string> PostSign(string type, int typeCode, string code)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var client = new HttpClient();
|
|
|
|
var request = new HttpRequestMessage(HttpMethod.Post, Parame.signUrl);
|
|
|
|
var content = new StringContent("{\r\n \"command\": \"sign\",\r\n \"command_num\": 111,\r\n \"data\": {\r\n \"operation\": \"" + type + "\",\r\n \"operation_code\": " + typeCode + ",\r\n \"parameters\": {\r\n \"data_type\": 1\r\n }\r\n }\r\n}", null, "application/json");
|
|
|
|
request.Content = content;
|
|
|
|
var response = await client.SendAsync(request);
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
if (response.StatusCode.ToString() == "OK")
|
|
|
|
{
|
|
|
|
if (type == "open" && typeCode == 1)
|
|
|
|
{
|
|
|
|
// 读取响应内容
|
|
|
|
string body = await response.Content.ReadAsStringAsync();
|
|
|
|
JObject jo = (JObject)JsonConvert.DeserializeObject(body);
|
|
|
|
string ret = jo["ret_info"].ToString();
|
|
|
|
JObject jo2 = (JObject)JsonConvert.DeserializeObject(ret);
|
|
|
|
if (jo2["code"].ToString() == "0")
|
|
|
|
{
|
|
|
|
//读取data数据
|
|
|
|
string data = jo["data"].ToString();
|
|
|
|
JObject jo3 = (JObject)JsonConvert.DeserializeObject(data);
|
|
|
|
//读取parameters数据
|
|
|
|
string parameters = jo3["parameters"].ToString();
|
|
|
|
JObject jo4 = (JObject)JsonConvert.DeserializeObject(parameters);
|
|
|
|
//读取签字base64
|
|
|
|
string sign_pic = jo4["sign_pic"].ToString();
|
|
|
|
string time = DateTime.Now.ToString("yyyyMMddHHmmss", DateTimeFormatInfo.InvariantInfo);
|
|
|
|
string dirpath = Utils.getSystemPaht() + @"/wwwroot/FileTxt";
|
|
|
|
if (!Directory.Exists(dirpath))
|
|
|
|
{
|
|
|
|
Directory.CreateDirectory(dirpath);
|
|
|
|
}
|
|
|
|
var tempFile = dirpath + "/" + time + ".txt";
|
|
|
|
using (StreamWriter sw = new StreamWriter(tempFile))
|
|
|
|
{
|
|
|
|
sw.Write(sign_pic);
|
|
|
|
sw.Close();
|
|
|
|
sw.Dispose();
|
|
|
|
}
|
|
|
|
string base64 = "";
|
|
|
|
string[] lines = File.ReadAllLines(tempFile);
|
|
|
|
foreach (string line in lines)
|
|
|
|
{
|
|
|
|
base64= base64+line;
|
|
|
|
}
|
|
|
|
File.Delete(tempFile);
|
|
|
|
//读取签字版主动回复数据
|
|
|
|
//"data:image/png;base64, " +
|
|
|
|
//Log.Info("读取签字版主动回复数据" + base64);
|
|
|
|
return "{\"keycode\":\"" + code + "\",\"message\":\"success\",\"code\":\"200\",\"status\":true,\"suffix\":\"png\",\"data\":\"" + base64 + "\"}";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//Log.Error("请求签字失败" + jo["message"].ToString());
|
|
|
|
return "{\"keycode\":\"" + code + "\",\"message\":\"Falied\",\"code\":\"400\",\"status\":false,\"suffix\":\"png\",\"data\":\"" + "签字失败" + "\"}";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return "{\"keycode\":\"" + code + "\",\"message\":\"success\",\"code\":\"200\",\"status\":true,\"suffix\":\"png\",\"data\":\"" + "关闭成功" + "\"}";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (type == "open" && typeCode == 1)
|
|
|
|
{
|
|
|
|
return "{\"keycode\":\"" + code + "\",\"message\":\"Falied\",\"code\":\"400\",\"status\":false,\"suffix\":\"png\",\"data\":\"" + "签字失败" + "\"}";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return "{\"keycode\":\"" + code + "\",\"message\":\"Falied\",\"code\":\"400\",\"status\":false,\"suffix\":\"png\",\"data\":\"" + "关闭签字失败" + "\"}";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
//Log.Error("签字异常:" + ex.Message);
|
|
|
|
return "{\"keycode\":\"" + code + "\",\"message\":\"Falied\",\"code\":\"400\",\"status\":false,\"suffix\":\"png\",\"data\":\"" + "签字异常" + "\"}";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|