using MES.Utility.Core; using System; using System.Collections.Generic; using 常用工具集.Base; using System.Threading; using 常用工具集.Utility.Network; using System.IO; using Ursa.Controls; using Avalonia.Threading; using Avalonia.Platform.Storage; namespace 常用工具集.ViewModel._02网络相关 { public class HTTP调试ViewModel : ViewModelBase { private bool getChecked = true; public bool GetChecked { get { return getChecked; } set { getChecked = value; if (value) { Visiable =false; } NotifyPropertyChanged(); } } private bool downloadChecked = false; public bool DownloadChecked { get { return downloadChecked; } set { downloadChecked = value; if (value) { Visiable = false; } NotifyPropertyChanged(); } } private bool postChecked = false; public bool PostChecked { get { return postChecked; } set { postChecked = value; if (value) { Visiable = true; } NotifyPropertyChanged(); } } public string Url { get; set; } = ""; public DelegateCommand ButtonCmd { get; set; } public bool JsonChecked { get; set; } = true; public bool FormChecked { get; set; } = false; public int Timeout { get; set; } = 1000; public bool Visiable { get; set; } = false; public string Parms { get; set; } = ""; public string Result { get; set; } = ""; public bool ButtonEnabled { get; set; } = true; public HTTP调试ViewModel() { ButtonCmd = new DelegateCommand(ButtonCmdFunc); } private void ButtonCmdFunc(object obj) { if (Url.IsNullOrEmpty()) { MessageBox.ShowAsync("请输入URL"); return; } string url = Url; if (!url.ToLower().StartsWith("http")) { url = "http://" + url; } int timeout = Timeout; if (GetChecked) { Dictionary dict = new Dictionary(); if (url.Contains("?")) { int index = url.IndexOf("?"); string parms = url.Substring(index + 1, url.Length - index - 1); string[] parmsArray = parms.Split('&'); foreach (string parm in parmsArray) { if (parm.Contains("=")) { string[] array = parm.Split('='); dict.Add(array[0], array[1]); } } parmsArray = Parms.Split('&'); foreach (string parm in parmsArray) { if (parm.Contains("=")) { string[] array = parm.Split('='); dict.Add(array[0], array[1]); } } url = url.Substring(0, index); } else { string[] parmsArray = Parms.Split('&'); foreach (string parm in parmsArray) { if (parm.Contains("=")) { string[] array = parm.Split('='); dict.Add(array[0], array[1]); } } } ButtonEnabled = false; Result = string.Empty; new Thread(() => { string ret = HttpUtils.DoGet(url, dict, timeout); if (!ret.IsNullOrEmpty()) { Result = ret; } else { Result = "网络或服务器异常"; } ButtonEnabled = true; }).Start(); } else if (DownloadChecked) { Dictionary dict = new Dictionary(); if (url.Contains("?")) { int index = url.IndexOf("?"); string parms = url.Substring(index + 1, url.Length - index - 1); string[] parmsArray = parms.Split('&'); foreach (string parm in parmsArray) { if (parm.Contains("=")) { string[] array = parm.Split('='); dict.Add(array[0], array[1]); } } parmsArray = Parms.Split('&'); foreach (string parm in parmsArray) { if (parm.Contains("=")) { string[] array = parm.Split('='); dict.Add(array[0], array[1]); } } url = url.Substring(0, index); } else { string[] parmsArray = Parms.Split('&'); foreach (string parm in parmsArray) { if (parm.Contains("=")) { string[] array = parm.Split('='); dict.Add(array[0], array[1]); } } } ButtonEnabled = false; Result = string.Empty; new Thread(() => { byte[] ret = HttpUtils.DoGetFile(url, timeout); if (ret == null) { Result = "网络或服务器异常"; } else { Dispatcher.UIThread.Invoke(async () => { var sp = GlobalValues.StorageProvider; if (sp is null) return; var result = await sp.SaveFilePickerAsync(new FilePickerSaveOptions() { Title = "保存文件" }); if (result == null) return; string filePath = result.Path.LocalPath; using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { fs.Write(ret, 0, ret.Length); } await MessageBox.ShowAsync("下载完成"); }); } ButtonEnabled = true; }).Start(); } else { //JSON if (JsonChecked) { ButtonEnabled = false; Result = string.Empty; new Thread(() => { string ret = HttpUtils.DoPostJson(url, Parms, timeout); if (!ret.IsNullOrEmpty()) { Result = ret; } else { Result = "网络或服务器异常"; } ButtonEnabled = true; }).Start(); } else { Dictionary dict = new Dictionary(); string[] parmsArray = Parms.Split('&'); foreach (string parm in parmsArray) { if (parm.Contains("=")) { string[] array = parm.Split('='); dict.Add(array[0], array[1]); } } ButtonEnabled = false; Result = string.Empty; new Thread(() => { string ret = HttpUtils.DoPostForm(url, dict, timeout); if (!ret.IsNullOrEmpty()) { Result = ret; } else { Result = "网络或服务器异常"; } ButtonEnabled = true; }).Start(); } } } } }