将XML请求发布到Web服务并接收响应

时间:2019-05-26 20:20:17

标签: c# xml web-services

我是新手,正在尝试理解将XML请求发送到Web服务并接收响应。经过一些阅读和研究之后,我最终得到的代码仍然没有运气,并且代码无法按预期工作,并且该URL试图调用返回No XML was recieved in HTTP POST.,有人可以帮助我或指出正确的方法吗?方向!提前致谢

我正在尝试从目录中读取XML文件,而不是发布到URL。

public class OrderRequest
    {
        private const string URL = "https://sitename/xml/xmlOrder/";

        private static void Main(string[] args)
        {
            Console.WriteLine("Starting..");
            var sourcePath = ".";

            while (true)
            {

                var directory = new DirectoryInfo(sourcePath);
                var files = directory.GetFiles("*.xml");

                if (files.Any())
                {
                    Console.WriteLine(files.Count() + " files found!");

                }

                foreach (var file in files)
                {
                    sourcePath = ".";
                    var RequestText = string.Empty;

                    try
                    {
                        Console.WriteLine("File: " + file.Name);

                        var responseText = string.Empty;
                        using (FileStream fs = file.OpenRead())
                        {
                            XmlDocument doc = new XmlDocument();
                            doc.Load(fs);
                            string xmlData = doc.InnerXml;
                            RequestText = xmlData;

                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
        }

        private static void CreateOrder(string orderRequestXml)
        {
            try
            {
                if (string.IsNullOrEmpty(orderRequestXml))
                {
                    throw new ArgumentException("XML data cannot be empty");
                }

                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(URL);
                webRequest.ContentType = "text/xml; encoding='utf-8'";
                webRequest.Method = "POST";

                byte[] data = Encoding.UTF8.GetBytes(orderRequestXml);

                using (Stream webStream = webRequest.GetRequestStream())
                {
                    webStream.Write(data, 0, data.Length);
                }

                using (HttpWebResponse webResponse = webRequest.GetResponse() as HttpWebResponse)
                {
                    if (webResponse.StatusCode == HttpStatusCode.OK)
                        Console.WriteLine("\r\nResponse Status Code is Ok and StatusDes is {0}", webResponse.StatusDescription);

                    using (Stream responseStream = webResponse.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(responseStream))
                        {
                            string xmlResponseString = reader.ReadToEnd();

                            XmlDocument xDoc = new XmlDocument();
                            xDoc.LoadXml(xmlResponseString);

                            Console.WriteLine(xmlResponseString);

                        }
                    }
                }

            }

            catch (WebException ex)
            {
                Console.WriteLine("\r\nWebException Raised. the following error occured : {0}", ex.Status);

            }

            catch (Exception ex)
            {

                Console.WriteLine("\nThe following Exception was raised : {0}", ex.Message);
            }

        }
    }

0 个答案:

没有答案