using System.Net.Http;
using System;
using System.Threading;
using Road_Test.Road.Tools.Singleton_Tool;
using Road_Test.Road.Log.Log_msg;
namespace Road_Test.Road.Tools.HttpUpload_Tool
{
class HttpUploadTool:Singleton<HttpUploadTool>
{
private const string url = @"https://mclasstest.club/roadTestApi/receiveFile";
public const int MaxFiledataLimit = 1048576;
public Form1 Form1;
public string dwFullPath = null;
public string snrFullPath = null;
public string startTime = null;
public string endTime = null;
public string deviceId = null;
private string HttpdwFullPath = null;
private string HttpsnrFullPath = null;
private string HttpstartTime = null;
private string HttpendTime = null;
private string HttpdeviceId = null;
private Thread thread;
public void UploadFileHttpRequest()
{
HttpdwFullPath = dwFullPath;
HttpsnrFullPath = snrFullPath;
HttpstartTime = startTime;
HttpendTime = endTime;
HttpdeviceId = deviceId;
dwFullPath = null;
snrFullPath = null;
ThreadStart thStart = new ThreadStart(StartUploadFileHttpRequest);
thread = new Thread(thStart);
thread.Priority = ThreadPriority.Highest;
thread.IsBackground = true;
thread.Start();
}
public void StartUploadFileHttpRequest()
{
using (HttpClient client = new HttpClient())
{
try
{
var content = new MultipartFormDataContent();
content.Add(new StringContent("dw"), "type");
content.Add(new StringContent(HttpstartTime), "startTime");
content.Add(new StringContent(HttpendTime), "endTime");
content.Add(new StringContent(HttpdeviceId), "设备id");
content.Add(new ByteArrayContent(System.IO.File.ReadAllBytes(HttpdwFullPath)), "dwFile", System.IO.Path.GetFileName(HttpdwFullPath));
content.Add(new ByteArrayContent(System.IO.File.ReadAllBytes(HttpsnrFullPath)), "snrFile", System.IO.Path.GetFileName(HttpsnrFullPath));
var result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
Form1.ShowData(result);
}
catch (Exception ex)
{
LogMSG.WriteLogmsg("Http数据上传异常", ex.Message.ToString());
}
try
{
thread.Abort();
}
catch (Exception)
{
}
}
}
}
}
namespace Road_Test.Road.Tools.Singleton_Tool
{
public class Singleton<T> where T : new()
{
private static T _instance;
public static T Instance
{
get
{
if (_instance==null)
{
_instance = new T();
}
return _instance;
}
}
}
}
|