IcsFromERPJob
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
4.8 KiB

  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 System.Xml.Linq;
  9. namespace ICSSoft.FromERP
  10. {
  11. public static class XmlAPIHelper
  12. {
  13. private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  14. public static string GetERPUrl()
  15. {
  16. return System.Configuration.ConfigurationManager.AppSettings["ERPUrl"];
  17. }
  18. public static string RequestXML(string url, string xmlData)
  19. {
  20. log.Debug(url);
  21. string statusCode = "400";
  22. string resultContent = "";
  23. try
  24. {
  25. log.Debug(xmlData);
  26. byte[] bytes = Encoding.UTF8.GetBytes(xmlData);
  27. ////Basic Auth
  28. //byte[] byteUser = Encoding.Default.GetBytes(userName + ":" + password);
  29. //string Authorization = Convert.ToBase64String(byteUser);
  30. WebRequest request = HttpWebRequest.Create(url);
  31. //避免远程连接证书无效问题
  32. ServicePointManager.ServerCertificateValidationCallback = (s, certificate, chain, errs) => true;
  33. request.Method = "POST";
  34. request.Timeout = 300000;
  35. request.ContentType = "text/xml;charset=UTF-8";
  36. request.ContentLength = bytes.Length;
  37. //request.Headers.Add("Authorization", "Basic " + Authorization);
  38. //根据soapui中的SOAPAction 进行赋值
  39. request.Headers.Add("SOAPAction", "\"\"");
  40. Stream requestStream = request.GetRequestStream();
  41. requestStream.Write(bytes, 0, bytes.Length);
  42. requestStream.Close();
  43. HttpWebResponse response = request.GetResponse() as HttpWebResponse;
  44. statusCode = response.StatusCode.ToString("d");
  45. Stream responseStream = response.GetResponseStream();
  46. StreamReader sr = new StreamReader(responseStream, Encoding.UTF8);
  47. resultContent = sr.ReadToEnd();
  48. sr.Dispose();
  49. responseStream.Close();
  50. //response.Dispose();
  51. log.Debug(resultContent);
  52. }
  53. catch (Exception ex)
  54. {
  55. log.Error(ex.ToString());
  56. statusCode = "400";
  57. resultContent = ex.Message;
  58. //throw ex;
  59. }
  60. return resultContent;
  61. }
  62. public static string GetData(string Operation, XElement document)
  63. {
  64. string result = string.Empty;
  65. try
  66. {
  67. XNamespace soapenvNamespace = "http://schemas.xmlsoap.org/soap/envelope/";
  68. XNamespace tipNamespace = "http://www.dsc.com.tw/tiptop/TIPTOPServiceGateWay";
  69. XElement root = new XElement(soapenvNamespace + "Envelope",
  70. new XAttribute(XNamespace.Xmlns + "soapenv", soapenvNamespace),
  71. new XAttribute(XNamespace.Xmlns + "tip", tipNamespace),
  72. new XElement(soapenvNamespace + "Header"),
  73. new XElement(soapenvNamespace + "Body",
  74. new XElement(tipNamespace + Operation + "Request",
  75. new XElement(tipNamespace + "request",
  76. new XElement("Request",
  77. new XElement("Access",
  78. new XElement("Authentication", new XAttribute("user", "kc"), new XAttribute("password", "kckckc258")),
  79. new XElement("Connection", new XAttribute("application", "ERP"), new XAttribute("source", "10.0.0.196")),
  80. new XElement("Organization", new XAttribute("name", "KC47")),
  81. new XElement("Locale", new XAttribute("language", "zh_cn"))
  82. ),
  83. new XElement("RequestContent",
  84. new XElement("Parameter"),
  85. document
  86. )
  87. )
  88. )
  89. )
  90. )
  91. );
  92. result = root.ToString().Replace("<tip:request>\r\n <Request>", "<tip:request><![CDATA[<Request>")
  93. .Replace("</Request>\r\n </tip:request>", "</Request>]]></tip:request>");
  94. }
  95. catch (Exception ex)
  96. {
  97. log.Error(ex.ToString());
  98. throw ex;
  99. }
  100. return result;
  101. }
  102. }
  103. }