如何将生成的XML发布到URL?

时间:2013-10-27 15:24:06

标签: c#

我正在尝试将生成的XML发送到URL,我一直在使用HttpWebResponse收到错误:

  

远程服务器返回错误:(417)期望失败

这是我的代码。

 //POST to URL 

            var httpRequest = (HttpWebRequest)WebRequest.Create("http://xxx.xxx.xxx.xxx:8000");
            httpRequest.Method = "POST";
            httpRequest.ContentType = "text/xml; charset=utf-8";
            httpRequest.ProtocolVersion = HttpVersion.Version11;


            //Set appropriate headers 

            var xmlWriterSettings = new XmlWriterSettings
            {
                NewLineHandling = NewLineHandling.None,
                Encoding = Encoding.ASCII
            };

            using (var requestStream = httpRequest.GetRequestStream())
            {
                xmlDoc.Save(requestStream);

            }

            using (var response = (HttpWebResponse)httpRequest.GetResponse())
            using (var responseStream = response.GetResponseStream())
            {
                // Response Code to see if the request was successful
                var responseXml = new XmlDocument();
                responseXml.Load(responseStream);
                using (var repp = XmlWriter.Create("response.xml"))
                {
                    responseXml.Save(repp);
                }
            }

0 个答案:

没有答案