|
|
|
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;
|
|
|
|
|
|
|
|
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);
|
|
|
|
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 PostSign(string type, string _callback)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
callback = _callback;
|
|
|
|
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: 1,\r\n parameters: {\r\n data_type: 1,\r\n title_name: \"\",\r\n }\r\n }\r\n }", null, "text/plain");
|
|
|
|
request.Content = content;
|
|
|
|
var response = await client.SendAsync(request);
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
if (response.StatusCode.ToString() == "OK")
|
|
|
|
{
|
|
|
|
// 读取响应内容
|
|
|
|
string body = await response.Content.ReadAsStringAsync();
|
|
|
|
Log.Info(body);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
Log.Error("签字版post请求: " + ex.Message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|