WcsApi.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using WCS.API.Helper;
  9. using WCS.API.Model;
  10. namespace WCS.API.WebApiClient
  11. {
  12. public class WcsApi
  13. {
  14. private static RestApi<DataResponse> restApi = new RestApi<DataResponse>();
  15. private static RestApi<DataResponseSPS> restApiSPS = new RestApi<DataResponseSPS>();
  16. private static RestApi<DataResponseMES> restApiMES = new RestApi<DataResponseMES>();
  17. public static bool Post(string url, object pars, object body, out string data, out string msg)
  18. {
  19. data = default(string);
  20. msg = default(string);
  21. try
  22. {
  23. DataResponse res = restApi.Post(url, pars, body, out string content);
  24. if (res.Success)
  25. {
  26. msg = res.Msg;
  27. data = res.Response;
  28. return res.Success;
  29. }
  30. }
  31. catch (Exception ex)
  32. {
  33. MsgDisplayHelper.Instance.DisplayError(ex.ToString());
  34. }
  35. return false;
  36. }
  37. public static void GetTrafficArea(string url, object pars, object body, out DataResponseTraffic res)
  38. {
  39. res = null;
  40. try
  41. {
  42. restApiSPS.Get(url, pars, body, out string content);
  43. }
  44. catch (Exception ex)
  45. {
  46. MsgDisplayHelper.Instance.DisplayError(ex.ToString());
  47. }
  48. }
  49. public static void PostSPS(string url, object pars, object body, out DataResponseSPS res)
  50. {
  51. res = null;
  52. try
  53. {
  54. res = restApiSPS.Post(url, pars, body, out string content);
  55. }
  56. catch (Exception ex)
  57. {
  58. MsgDisplayHelper.Instance.DisplayError(ex.ToString());
  59. }
  60. }
  61. public static void PostMES(string url, object pars, object body, out DataResponseMES res)
  62. {
  63. res = null;
  64. try
  65. {
  66. res = restApiMES.Post(url, pars, body, out string content);
  67. }
  68. catch (Exception ex)
  69. {
  70. MsgDisplayHelper.Instance.DisplayError(ex.ToString());
  71. }
  72. }
  73. public static String GetData(String url)
  74. {
  75. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  76. request.Method = "GET";
  77. request.ContentType = "text/html, application/xhtml+xml, */*";
  78. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  79. Stream rs = response.GetResponseStream();
  80. StreamReader sr = new StreamReader(rs, Encoding.UTF8);
  81. var result = sr.ReadToEnd();
  82. sr.Close();
  83. rs.Close();
  84. return result;
  85. }
  86. }
  87. }