UpgradeHelper.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Net.Http;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace MV485.Upgrade
  10. {
  11. public class UpgradeHelper
  12. {
  13. //升级包所在的位置
  14. public const string UpgradeUrl = @"https://www.xzeai.com:6090/download/upgrade/MV485_app/";
  15. public static UpgradeModel ReadUpdateTxt1()
  16. {
  17. string updateTxtUrl = Path.Combine(UpgradeUrl, "upgrade.txt");
  18. try
  19. {
  20. WebClient wc = new WebClient();
  21. wc.Headers.Add("Accept-Charset", "utf-8");
  22. byte[] buffer = wc.DownloadData(updateTxtUrl);
  23. string res = Encoding.UTF8.GetString(buffer);
  24. //Encoding.UTF8.GetString(buffer);
  25. Console.WriteLine(res);
  26. UpgradeModel upgradeModel = UpgradeModel.ParseModel(res);
  27. if (upgradeModel == null)
  28. {
  29. throw new InvalidOperationException("无法读正常读取升级信息。");
  30. }
  31. return upgradeModel;
  32. }
  33. catch (WebException ex)
  34. {
  35. //ex.Message = "远程服务器返回错误: (404) 未找到。"
  36. Console.WriteLine(ex.Message);
  37. throw new InvalidOperationException("无法读取升级信息。");
  38. }
  39. catch (Exception e)
  40. {
  41. Console.WriteLine(e.Message);
  42. throw new InvalidOperationException($"读取升级包错误{e.Message}。");
  43. }
  44. }
  45. public static UpgradeModel ReadUpdateTxt()
  46. {
  47. string updateTxtUrl = Path.Combine(UpgradeUrl, "upgrade.txt");
  48. try
  49. {
  50. // 使用 HttpClient 代替 WebClient
  51. using (HttpClient client = new HttpClient())
  52. {
  53. // 异步请求并获取响应
  54. HttpResponseMessage response = client.GetAsync(updateTxtUrl).Result;
  55. // 如果请求失败,抛出异常
  56. if (!response.IsSuccessStatusCode)
  57. {
  58. throw new InvalidOperationException($"无法读取升级信息,HTTP 错误:{response.StatusCode}");
  59. }
  60. // 获取响应内容并转换为字符串,注意这里可以指定编码
  61. string res = response.Content.ReadAsStringAsync().Result;
  62. Console.WriteLine(res);
  63. // 调用解析方法,转换为模型
  64. UpgradeModel upgradeModel = UpgradeModel.ParseModel(res);
  65. // 校验解析是否成功
  66. if (upgradeModel == null || string.IsNullOrEmpty(upgradeModel.VersionCode) || string.IsNullOrEmpty(upgradeModel.ApkName))
  67. {
  68. throw new InvalidOperationException("升级信息格式错误或缺失必要数据。");
  69. }
  70. return upgradeModel;
  71. }
  72. }
  73. catch (WebException ex)
  74. {
  75. // 提供更多的详细错误信息
  76. Console.WriteLine($"WebException: {ex.Message}");
  77. throw new InvalidOperationException($"无法读取升级信息,错误: {ex.Message}");
  78. }
  79. catch (HttpRequestException ex)
  80. {
  81. // 针对 Http 请求的异常做进一步处理
  82. Console.WriteLine($"HttpRequestException: {ex.Message}");
  83. throw new InvalidOperationException($"无法从服务器获取数据,错误: {ex.Message}");
  84. }
  85. catch (Exception e)
  86. {
  87. // 捕获其他未知异常
  88. Console.WriteLine($"Exception: {e.Message}");
  89. throw new InvalidOperationException($"读取升级包错误:{e.Message}");
  90. }
  91. }
  92. public static string GetCurrentVersion()
  93. {
  94. // 获取当前应用的版本号,返回格式为 "1006"
  95. var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
  96. return version.ToString();
  97. }
  98. public static bool IsNewVersionAvailable(string currentVersion, string serverVersion)
  99. {
  100. var current = new Version(currentVersion);
  101. var server = new Version(serverVersion);
  102. return server.CompareTo(current) > 0; // 如果服务器版本大于当前版本,则返回 true
  103. }
  104. //--------------------------------------------
  105. }
  106. }