SoapHandler.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Threading.Tasks;
  8. using System.Xml;
  9. using log4net;
  10. namespace Common.Soap
  11. {
  12. public class CommonSoapHandler
  13. {
  14. private static readonly log4net.ILog log = log4net.LogManager.GetLogger(
  15. typeof(CommonSoapHandler)
  16. );
  17. public static async Task<string> ExecuteAsync(string url, string soapBody)
  18. {
  19. try
  20. {
  21. log.Debug($"url: {url} => soapBody: {@soapBody.Replace("\n", "")}");
  22. // Convert the envelope to a byte array
  23. var content = new StringContent(
  24. @soapBody.Replace("\n", ""),
  25. Encoding.UTF8,
  26. "text/xml"
  27. );
  28. // Send the request
  29. using (var client = new HttpClient())
  30. {
  31. client.Timeout = TimeSpan.FromSeconds(120);
  32. // Make the POST request
  33. var response = await client.PostAsync(url, content);
  34. // Read the response content
  35. var responseContent = await response.Content.ReadAsStringAsync();
  36. log.Debug($"res: {responseContent}");
  37. return responseContent;
  38. }
  39. }
  40. catch (Exception ex)
  41. {
  42. log.Error("Exception: ", ex);
  43. }
  44. return "Error";
  45. }
  46. public static HttpWebRequest CreateWebRequest(string url)
  47. {
  48. HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
  49. webRequest.Headers.Add(@"SOAP:Action");
  50. webRequest.ContentType = "text/xml;charset=\"utf-8\"";
  51. webRequest.Accept = "text/xml";
  52. webRequest.Method = "POST";
  53. return webRequest;
  54. }
  55. public static (string, string) GetResultFromSoap(string data)
  56. {
  57. Regex regex = new Regex(
  58. $"""(?<=<return>)(.*?)(?=<\/return>)""",
  59. RegexOptions.IgnoreCase
  60. );
  61. Match match;
  62. string result = "-1|Error";
  63. for (match = regex.Match(data); match.Success; match = match.NextMatch())
  64. {
  65. result = match.Groups[0].Value;
  66. }
  67. log.Debug($"result regex: {result}");
  68. if (result.Contains("|"))
  69. {
  70. string[] resultSepa = result.Split("|");
  71. return (resultSepa[0], resultSepa[1]);
  72. }
  73. else if (result.Contains("errorCode"))
  74. {
  75. string resultErrorCode = "";
  76. regex = new Regex(
  77. $"""(?<=<errorCode>)(.*?)(?=<\/errorCode>)""",
  78. RegexOptions.IgnoreCase
  79. );
  80. for (match = regex.Match(result); match.Success; match = match.NextMatch())
  81. {
  82. resultErrorCode = match.Groups[0].Value;
  83. }
  84. log.Debug($"resultErrorCode regex: {resultErrorCode}");
  85. return (resultErrorCode, "");
  86. }
  87. else
  88. {
  89. return (result, "Success");
  90. }
  91. }
  92. }
  93. }