You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.8 KiB

  1. using Newtonsoft.Json;
  2. using OT.COM.SignalerMessage;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Net;
  7. using System.Text;
  8. namespace TruckAP
  9. {
  10. public class CReqestItem
  11. {
  12. public CRequestMessage Req { get; set; }
  13. }
  14. public class CReqestPack
  15. {
  16. public List<CReqestItem> Reqs { get; set; }
  17. public string Token { get; set; }
  18. }
  19. public class CResponsePack
  20. {
  21. public List<CResponseMessage> Reps { get; set; }
  22. }
  23. public class SendWebApiRequest
  24. {
  25. public string RunEncryptRequest(string i_sURL, CReqestPack i_crm, out string o_sRes)
  26. {
  27. string sData = JsonConvert.SerializeObject(i_crm);
  28. return runRequest(i_sURL, sData, out o_sRes);
  29. }
  30. protected string runRequest(string i_sURL, string i_sContent, out string o_sRes,
  31. [System.Runtime.CompilerServices.CallerLineNumber] int i_nCodeLine = 0,
  32. [System.Runtime.CompilerServices.CallerMemberName] string i_sMemberName = "",
  33. [System.Runtime.CompilerServices.CallerFilePath] string i_sSourcePath = "")
  34. {
  35. string sRes = null;
  36. Stream dataStream = null;
  37. WebResponse response = null;
  38. StreamReader reader = null;
  39. string sMsg = null;
  40. try
  41. {
  42. // https://msdn.microsoft.com/zh-tw/library/debx8sh9(v=VS.110).aspx
  43. // Create a request using a URL that can receive a post.
  44. WebRequest request = WebRequest.Create(i_sURL);
  45. // Set the Method property of the request to POST.
  46. request.Method = "POST";
  47. // Create POST data and convert it to a byte array.
  48. request.UseDefaultCredentials = true;
  49. string postData = i_sContent;
  50. byte[] byteArray = Encoding.UTF8.GetBytes(postData);
  51. // Set the ContentType property of the WebRequest.
  52. request.ContentType = "application/json";
  53. // Set the ContentLength property of the WebRequest.
  54. request.ContentLength = byteArray.Length;
  55. // Get the request stream.
  56. dataStream = request.GetRequestStream();
  57. // Write the data to the request stream.
  58. dataStream.Write(byteArray, 0, byteArray.Length);
  59. // Close the Stream object.
  60. dataStream.Close();
  61. // Get the response.
  62. response = request.GetResponse();
  63. // Display the status.
  64. // Get the stream containing content returned by the server.
  65. dataStream = response.GetResponseStream();
  66. // Open the stream using a StreamReader for easy access.
  67. reader = new StreamReader(dataStream);
  68. // Read the content.
  69. string responseFromServer = reader.ReadToEnd();
  70. // Display the content.
  71. sRes = responseFromServer;
  72. }
  73. catch (Exception ex)
  74. {
  75. sMsg = $"{nameof(runRequest)} unknwon exception. i_sURL={i_sURL}, i_sContent={i_sContent}. Call from {i_sMemberName} {i_sSourcePath}({i_nCodeLine}).";
  76. #if DEBUG
  77. sMsg += $" Exception={ex.Message}";
  78. System.Diagnostics.Debug.WriteLine(sMsg);
  79. #endif
  80. }
  81. finally
  82. {
  83. if (null != reader)
  84. {
  85. reader.Close();
  86. }
  87. if (null != response)
  88. {
  89. response.Close();
  90. }
  91. if (null != dataStream)
  92. {
  93. dataStream.Close();
  94. }
  95. }
  96. o_sRes = sRes;
  97. return sMsg;
  98. }
  99. }
  100. }