using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using System.Xml.Linq; namespace ICSSoft.FromERP { public static class XmlAPIHelper { private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); public static string GetERPUrl() { return System.Configuration.ConfigurationManager.AppSettings["ERPUrl"]; } public static string RequestXML(string url, string xmlData) { log.Debug(url); string statusCode = "400"; string resultContent = ""; try { log.Debug(xmlData); byte[] bytes = Encoding.UTF8.GetBytes(xmlData); ////Basic Auth //byte[] byteUser = Encoding.Default.GetBytes(userName + ":" + password); //string Authorization = Convert.ToBase64String(byteUser); WebRequest request = HttpWebRequest.Create(url); //避免远程连接证书无效问题 ServicePointManager.ServerCertificateValidationCallback = (s, certificate, chain, errs) => true; request.Method = "POST"; request.Timeout = 300000; request.ContentType = "text/xml;charset=UTF-8"; request.ContentLength = bytes.Length; //request.Headers.Add("Authorization", "Basic " + Authorization); //根据soapui中的SOAPAction 进行赋值 request.Headers.Add("SOAPAction", "\"\""); Stream requestStream = request.GetRequestStream(); requestStream.Write(bytes, 0, bytes.Length); requestStream.Close(); HttpWebResponse response = request.GetResponse() as HttpWebResponse; statusCode = response.StatusCode.ToString("d"); Stream responseStream = response.GetResponseStream(); StreamReader sr = new StreamReader(responseStream, Encoding.UTF8); resultContent = sr.ReadToEnd(); sr.Dispose(); responseStream.Close(); //response.Dispose(); log.Debug(resultContent); } catch (Exception ex) { log.Error(ex.ToString()); statusCode = "400"; resultContent = ex.Message; //throw ex; } return resultContent; } public static string GetData(string Operation, XElement document) { string result = string.Empty; try { XNamespace soapenvNamespace = "http://schemas.xmlsoap.org/soap/envelope/"; XNamespace tipNamespace = "http://www.dsc.com.tw/tiptop/TIPTOPServiceGateWay"; XElement root = new XElement(soapenvNamespace + "Envelope", new XAttribute(XNamespace.Xmlns + "soapenv", soapenvNamespace), new XAttribute(XNamespace.Xmlns + "tip", tipNamespace), new XElement(soapenvNamespace + "Header"), new XElement(soapenvNamespace + "Body", new XElement(tipNamespace + Operation + "Request", new XElement(tipNamespace + "request", new XElement("Request", new XElement("Access", new XElement("Authentication", new XAttribute("user", "kc"), new XAttribute("password", "kckckc258")), new XElement("Connection", new XAttribute("application", "ERP"), new XAttribute("source", "10.0.0.196")), new XElement("Organization", new XAttribute("name", "KC47")), new XElement("Locale", new XAttribute("language", "zh_cn")) ), new XElement("RequestContent", new XElement("Parameter"), document ) ) ) ) ) ); result = root.ToString().Replace("\r\n ", "") .Replace("\r\n ", "]]>"); } catch (Exception ex) { log.Error(ex.ToString()); throw ex; } return result; } } }