System.Net.ProtocolViolationException: You must write ContentLength bytes to the request stream before calling [Begin]GetResponse

时间:2015-07-08 15:49:07

标签: c# exception webrequest content-length

I am getting the

"System.Net.ProtocolViolationException: You must write ContentLength bytes to the request stream before calling [Begin]GetResponse" error when calling to the "BeginGetResponse" method of the web request.

This is my code:

try
{
    Stream dataStream = null;
    WebRequest Webrequest;
    Webrequest = WebRequest.Create(this.EndPointAddress);
    Webrequest.Credentials = new NetworkCredential(this.username, this.password);

    Webrequest.ContentType = "text/xml";
    Webrequest.Method = WebRequestMethods.Http.Post;                    

    byteArray = System.Text.Encoding.UTF8.GetBytes(xmlRequest.Children[0].InnerXML);

    Webrequest.ContentLength = byteArray.Length;

    dataStream = Webrequest.GetRequestStream();
    dataStream.Write(byteArray, 0, byteArray.Length);                

    RequestState rs = new RequestState();
    rs.Request = Webrequest;                    

    IAsyncResult r = (IAsyncResult)Webrequest.BeginGetResponse(new AsyncCallback(RespCallback), rs);
}
catch (Exception exc)
{                    
    TRACE.EXCEPTION(exc);
}
finally
{
    dataStream.Close();
}

More specifically, after calling to the function "getRequestStream()", the Stream is throwing this exception for the lenght:

'stream.Length' threw an exception of type 'System.NotSupportedException'

What could be causing it?

2 个答案:

答案 0 :(得分:3)

您的代码应该适用于.NET 2.0 4.0及更高版本,您应该在写完后关闭该流:

<body>
<div class="site-container">
    <div style="min-height: 700px;">
        <div class="container">
            <div class="slider">
                <div class="slider-container">
                    <div class="slider-content">
                        <div class="slider-content-wrapper">

                            <a href="#" class="slider-item product-item">
                                <img src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcRu0ngHBYiMliunAqW5pJ4f-KuiWapDqBJkb3aIpz33a506cpOg0A" alt="">

                            </a>
                            <a href="#" class="slider-item product-item">
                                <img src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcRu0ngHBYiMliunAqW5pJ4f-KuiWapDqBJkb3aIpz33a506cpOg0A">

                            </a>
                            <a href="#" class="slider-item product-item">
                                <img src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcRu0ngHBYiMliunAqW5pJ4f-KuiWapDqBJkb3aIpz33a506cpOg0A">

                            </a>

                        </div>
                    </div>
                </div>
            </div>

            <div class="slider" style="background-color: red;" id="thatDivMustHaveTheSameWidthWithContainer">
                <div class="slider-container">
                    <div class="slider-content">
                        <div class="slider-content-wrapper">

                            <a href="#" class="slider-item product-item">
                                <img src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcRu0ngHBYiMliunAqW5pJ4f-KuiWapDqBJkb3aIpz33a506cpOg0A" alt="">

                            </a>
                            <a href="#" class="slider-item product-item">
                                <img src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcRu0ngHBYiMliunAqW5pJ4f-KuiWapDqBJkb3aIpz33a506cpOg0A">

                            </a>
                            <a href="#" class="slider-item product-item">
                                <img src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcRu0ngHBYiMliunAqW5pJ4f-KuiWapDqBJkb3aIpz33a506cpOg0A">

                            </a>

                        </div>
                    </div>
                </div>
            </div>

            <div class="slider">
                <div class="slider-container">
                    <div class="slider-content">
                        <div class="slider-content-wrapper">

                            <a href="#" class="slider-item product-item">
                                <img src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcRu0ngHBYiMliunAqW5pJ4f-KuiWapDqBJkb3aIpz33a506cpOg0A">

                            </a>
                            <a href="#" class="slider-item product-item">
                                <img src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcRu0ngHBYiMliunAqW5pJ4f-KuiWapDqBJkb3aIpz33a506cpOg0A" alt="">

                            </a>
                            <a href="#" class="slider-item product-item">
                                <img src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcRu0ngHBYiMliunAqW5pJ4f-KuiWapDqBJkb3aIpz33a506cpOg0A">

                            </a>
                        </div>
                    </div>
                </div>
            </div>

        </div>
    </div>
</div>
</body>

答案 1 :(得分:0)

检查以确认您的服务器已设置为接受大文件。您可能会发现您遇到了4兆的默认限制。

将以下内容添加到您的web.config文件中以进行更大的文件上传:

<system.webServer>
    <security>
        <requestFiltering>
            <requestLimits maxAllowedContentLength="104857600" />
        </requestFiltering>
    </security>
</system.webServer>