ASP.NET ProtocolViolationException - 无法发送内容体与这个动词型 [英] ASP.NET ProtocolViolationException - Cannot send a content-body with this verb-type

查看:1985
本文介绍了ASP.NET ProtocolViolationException - 无法发送内容体与这个动词型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么会 req.GetRequestStream()关闭(); 的原因。 ProtocolViolationException - 无法发送内容体与这个动词型在code段是从这里。谢谢你。

  WebRequest的REQ = NULL;
        WebResponse类RSP = NULL;
        尝试
        {
            字符串文件名=Login.xml;
            字符串URI =HTTP://本地主机/ API /登录;
            REQ = WebRequest.Create(URI);            //req.Proxy = WebProxy.GetDefaultProxy(); //启用如果使用代理
            req.Method =POST; // Post方法
            req.Co​​ntentType =文/ XML; // 内容类型            //使用基于文本的作家包裹请求流
            StreamWriter的作家=新的StreamWriter(req.GetRequestStream());            //写XML文本流
            writer.WriteLine(this.GetTextFromXMLFile(文件名));
            writer.Close();            //将数据发送到Web服务器
            RSP = req.GetResponse();        }
        赶上(WEBEX引发WebException)
        {
            LOG.Error(webEx.StackTrace.ToString());
        }
        赶上(异常前)
        {
            LOG.Error(ex.StackTrace.ToString());
        }
        最后
        {
            。如果(!REQ = NULL)req.GetRequestStream()关闭();
            如果(RSP!= NULL)rsp.GetResponseStream()关闭()。
        }


解决方案

您是否尝试过使用 req.Co​​ntentType =应用程序/ XML 而不是正文/ XML

Why will req.GetRequestStream().Close(); cause "ProtocolViolationException - Cannot send a content-body with this verb-type." The code snippet is from here. Thanks.

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

解决方案

Have you tried using req.ContentType = application/xml instead of text/xml?

这篇关于ASP.NET ProtocolViolationException - 无法发送内容体与这个动词型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆