【问题标题】:ASP.NET ProtocolViolationException - Cannot send a content-body with this verb-typeASP.NET ProtocolViolationException - 无法发送具有此动词类型的内容主体
【发布时间】:2011-06-28 23:23:36
【问题描述】:

为什么 req.GetRequestStream().Close(); 会导致“ProtocolViolationException - 无法发送具有此动词类型的内容主体。”代码 sn-p 来自here。谢谢。

        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();
        }

【问题讨论】:

  • 这与 ASP.NET 有什么关系?

标签: c# asp.net


【解决方案1】:

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

【讨论】:

  • 我也有类似的问题,req.ContentType = application/xml 没有解决。
猜你喜欢
  • 2011-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多