248 lines
8.1 KiB
C#
248 lines
8.1 KiB
C#
using FileProcessClient.domain;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using System.Diagnostics;
|
|
using System.Net;
|
|
using System.Net.Http.Headers;
|
|
using System.Text.Json.Nodes;
|
|
|
|
namespace edu.huvtc.utils
|
|
{
|
|
internal class HttpUtils
|
|
{
|
|
|
|
public static async Task<string> HttpClientPost(string url, string data)
|
|
{
|
|
string token = CommonUtils.getToken();
|
|
|
|
var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.None };
|
|
|
|
handler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
|
|
|
|
string serverUrl = CommonUtils.getServerUrl();
|
|
|
|
using (var httpclient = new HttpClient(handler))
|
|
{
|
|
try
|
|
{
|
|
httpclient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
|
|
|
|
httpclient.BaseAddress = new Uri(serverUrl + url);
|
|
|
|
HttpContent content = new StringContent(data, System.Text.Encoding.UTF8, "application/json");
|
|
|
|
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
|
|
|
|
var response = await httpclient.PostAsync(url, content);
|
|
|
|
string responseString = await response.Content.ReadAsStringAsync();
|
|
|
|
//Trace.WriteLine(responseString);
|
|
|
|
return responseString;
|
|
}
|
|
catch (HttpRequestException e)
|
|
{
|
|
// 捕获 HttpRequestException 异常以处理网络错误
|
|
//Console.WriteLine($"请求异常: {e.Message}");
|
|
MessageBox.Show("网络异常,请确认是否联网!");
|
|
return "";
|
|
}
|
|
catch (TaskCanceledException e)
|
|
{
|
|
MessageBox.Show("请求超时!");
|
|
return "";
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
// 捕获其他异常
|
|
//Console.WriteLine($"其他异常: {e.Message}");
|
|
MessageBox.Show("发生未知错误");
|
|
return "503";
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public static async Task<string> HttpClientGet(string url, string query)
|
|
{
|
|
string token = CommonUtils.getToken();
|
|
|
|
url = CommonUtils.getServerUrl() + url;
|
|
|
|
Trace.WriteLine(url);
|
|
|
|
using (HttpClient client = new HttpClient())
|
|
{
|
|
try
|
|
{
|
|
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
|
|
|
|
var builder = new UriBuilder(url);
|
|
|
|
//var query = System.Web.HttpUtility.ParseQueryString(builder.Query);
|
|
//query["param1"] = "value1";
|
|
//query["param2"] = "value2";
|
|
|
|
builder.Query = query;
|
|
|
|
string str_url = builder.ToString();
|
|
|
|
HttpResponseMessage response = await client.GetAsync(str_url);
|
|
|
|
|
|
return await response.Content.ReadAsStringAsync();
|
|
}
|
|
catch (HttpRequestException e)
|
|
{
|
|
// 捕获 HttpRequestException 异常以处理网络错误
|
|
//Console.WriteLine($"请求异常: {e.Message}");
|
|
MessageBox.Show("网络或服务器异常!");
|
|
return "";
|
|
}
|
|
catch (TaskCanceledException e)
|
|
{
|
|
MessageBox.Show("请求超时!");
|
|
return "";
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
// 捕获其他异常
|
|
//Console.WriteLine($"其他异常: {e.Message}");
|
|
MessageBox.Show("发生未知错误");
|
|
return "";
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public static async Task<bool> getUserInfo()
|
|
{
|
|
string token = CommonUtils.getToken();
|
|
//Trace.WriteLine(token);
|
|
if (token != null && token != "")
|
|
{
|
|
string url = CommonUtils.getApiUrl("user_info_url");
|
|
string result = await HttpUtils.HttpClientGet(url, "");
|
|
|
|
//Trace.WriteLine(result);
|
|
if (result == "")
|
|
{
|
|
return false;
|
|
}
|
|
JObject jsonObject = JObject.Parse(result);
|
|
int code = (int)jsonObject["code"]!;
|
|
if (code != 200)
|
|
{
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
string strUser = jsonObject["user"]!.ToString();
|
|
|
|
//Trace.WriteLine(strUser);
|
|
|
|
JObject obj = JObject.Parse(strUser);
|
|
int id = (int)obj["userId"]!;
|
|
string loginId = (string)obj["userName"]!;
|
|
string phone = (string)obj["phonenumber"]!;
|
|
string name = (string)obj["nickName"]!;
|
|
int deptId = (int)obj["deptId"]!;
|
|
User user = new User(id, name, loginId, phone, deptId);
|
|
CommonUtils.setUser(user);
|
|
return true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static async Task<bool> saveMediaFileInfo(MediaFileInfo mediaFileInfo)
|
|
{
|
|
var jsonString = JsonConvert.SerializeObject(mediaFileInfo);
|
|
//Trace.WriteLine(jsonString);
|
|
string url = CommonUtils.getApiUrl("save_media_file_url");
|
|
string res = await HttpClientPost(url, jsonString);
|
|
if (res == "")
|
|
{
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public static async Task<List<ComboboxItem>> getVersionList()
|
|
{
|
|
string url = CommonUtils.getApiUrl("file_version_list_url");
|
|
|
|
string res = await HttpUtils.HttpClientGet(url, "");
|
|
//Trace.WriteLine(res);
|
|
if (res == null)
|
|
{
|
|
return new List<ComboboxItem>();
|
|
}
|
|
|
|
List<ComboboxItem> list = new List<ComboboxItem>();
|
|
JArray array = JArray.Parse(res);
|
|
|
|
foreach (var item in array)
|
|
{
|
|
list.Add(new ComboboxItem(item["dictLabel"]!.ToString(), item["dictCode"]!.ToString()));
|
|
}
|
|
|
|
return list;
|
|
}
|
|
|
|
public static async Task<List<ComboboxItem>> getCameraList()
|
|
{
|
|
string url = CommonUtils.getApiUrl("camera_list_url");
|
|
|
|
string res = await HttpUtils.HttpClientGet(url, "");
|
|
if (res == null)
|
|
{
|
|
return new List<ComboboxItem>();
|
|
}
|
|
|
|
List<ComboboxItem> list = new List<ComboboxItem>();
|
|
|
|
JArray array = JArray.Parse(res);
|
|
|
|
foreach (var item in array)
|
|
{
|
|
list.Add(new ComboboxItem(item["duid"]!.ToString(), item["id"]!.ToString()));
|
|
}
|
|
|
|
return list;
|
|
}
|
|
|
|
public static async Task<bool> validateLogin(string username, string password)
|
|
{
|
|
JsonObject obj = new JsonObject();
|
|
obj.Add("username", username);
|
|
obj.Add("password", password);
|
|
obj.Add("loginType", 1);
|
|
string result = await HttpUtils.HttpClientPost("/login", obj.ToString());
|
|
Trace.WriteLine("==================="+result);
|
|
if (result == "")
|
|
{
|
|
return false;
|
|
}
|
|
JObject jsonObject = JObject.Parse(result);
|
|
|
|
int code = (int)jsonObject["code"]!;
|
|
if (code == 200)
|
|
{
|
|
string token = (string)jsonObject["token"]!;
|
|
CommonUtils.setToken(token);
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|