115 lines
3.9 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 JeepConsole
  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. string responseFromServer = null;
  41. try
  42. {
  43. // https://msdn.microsoft.com/zh-tw/library/debx8sh9(v=VS.110).aspx
  44. // Create a request using a URL that can receive a post.
  45. WebRequest request = WebRequest.Create(i_sURL);
  46. request.Timeout = 600000;
  47. // Set the Method property of the request to POST.
  48. request.Method = "POST";
  49. // Create POST data and convert it to a byte array.
  50. request.UseDefaultCredentials = true;
  51. string postData = i_sContent;
  52. byte[] byteArray = Encoding.UTF8.GetBytes(postData);
  53. // Set the ContentType property of the WebRequest.
  54. request.ContentType = "application/json";
  55. // Set the ContentLength property of the WebRequest.
  56. request.ContentLength = byteArray.Length;
  57. // Get the request stream.
  58. dataStream = request.GetRequestStream();
  59. // Write the data to the request stream.
  60. dataStream.Write(byteArray, 0, byteArray.Length);
  61. // Close the Stream object.
  62. dataStream.Close();
  63. // Get the response.
  64. response = request.GetResponse();
  65. // Display the status.
  66. // Get the stream containing content returned by the server.
  67. dataStream = response.GetResponseStream();
  68. // Open the stream using a StreamReader for easy access.
  69. reader = new StreamReader(dataStream);
  70. // Read the content.
  71. responseFromServer = reader.ReadToEnd();
  72. // Display the content.
  73. sRes = responseFromServer;
  74. }
  75. catch (Exception ex)
  76. {
  77. sMsg = $"{nameof(runRequest)} unknwon exception. i_sURL={i_sURL}, i_sContent={i_sContent}. Call from {i_sMemberName} {i_sSourcePath}({i_nCodeLine}).";
  78. sMsg += ex.StackTrace;
  79. sMsg += " " + responseFromServer;
  80. #if DEBUG
  81. System.Diagnostics.Debug.WriteLine(sMsg);
  82. #endif
  83. }
  84. finally
  85. {
  86. if (null != reader)
  87. {
  88. reader.Close();
  89. }
  90. if (null != response)
  91. {
  92. response.Close();
  93. }
  94. if (null != dataStream)
  95. {
  96. dataStream.Close();
  97. }
  98. }
  99. o_sRes = sRes;
  100. return sMsg;
  101. }
  102. }
  103. }