ASP.NET ProtocolViolationException - 无法发送具有此动词类型的内容主体

2024-03-05

为什么会req.GetRequestStream().Close(); cause "协议违规异常- 无法发送具有此动词类型的内容主体。” 代码片段来自here http://www.codeproject.com/KB/dotnet/XmlPost.aspx。谢谢。

        WebRequest req = null;
        WebResponse rsp = null;
        try
        {
            string fileName = "Login.xml";
            string uri = "http://localhost/api/login";
            req = WebRequest.Create(uri);

            //req.Proxy = WebProxy.GetDefaultProxy(); // Enable if using proxy
            req.Method = "POST";        // Post method
            req.ContentType = "text/xml";     // content type

            // Wrap the request stream with a text-based writer
            StreamWriter writer = new StreamWriter(req.GetRequestStream());

            // Write the XML text into the stream
            writer.WriteLine(this.GetTextFromXMLFile(fileName));
            writer.Close();

            // Send the data to the webserver
            rsp = req.GetResponse();

        }
        catch (WebException webEx)
        {
            LOG.Error(webEx.StackTrace.ToString());
        }
        catch (Exception ex)
        {
            LOG.Error(ex.StackTrace.ToString());
        }
        finally
        {
            if (req != null) req.GetRequestStream().Close();
            if (rsp != null) rsp.GetResponseStream().Close();
        }

你有没有尝试过使用req.ContentType = application/xml代替text/xml?

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ASP.NET ProtocolViolationException - 无法发送具有此动词类型的内容主体 的相关文章

随机推荐