C# ASP.NET ProtocolViolationException-无法发送具有此谓词类型的内容正文

C# ASP.NET ProtocolViolationException-无法发送具有此谓词类型的内容正文,c#,asp.net,C#,Asp.net,为什么req.GetRequestStream().Close()原因“ProtocolViolationException-无法发送此动词类型的内容正文。”代码段来自。谢谢 您是否尝试过使用req.ContentType=application/xml而不是text/xml?这与ASP.NET有什么关系?我有一个类似的问题,req.ContentType=application/xml无法解决。 WebRequest req = null; WebRespons

为什么req.GetRequestStream().Close()原因“ProtocolViolationException-无法发送此动词类型的内容正文。”代码段来自。谢谢


您是否尝试过使用
req.ContentType=application/xml
而不是
text/xml

这与ASP.NET有什么关系?我有一个类似的问题,
req.ContentType=application/xml
无法解决。
        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();
        }