首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >System.Net.HttpWebRequest.GetResponse()错误“您必须在调用[Begin]GetResponse之前将ContentLength字节写入请求流。”

System.Net.HttpWebRequest.GetResponse()错误“您必须在调用[Begin]GetResponse之前将ContentLength字节写入请求流。”
EN

Stack Overflow用户
提问于 2019-01-08 05:28:25
回答 1查看 1.2K关注 0票数 0

我有一个上传文件到我们的服务器的方法。我在尝试上载大文件时看到标题中的错误。我尝试在web.config中从35480 KB增加maxRequestLength,但仍然看到错误。

代码语言:javascript
运行
复制
<httpRuntime maxRequestLength="3548000" executionTimeout="1000" targetFramework="4.6.1"/>

我也尝试过在这篇文章System.Net.ProtocolViolationException: You must write ContentLength bytes to the request stream before calling [Begin]GetResponse中提到的using语句中发出请求,但都无济于事。任何建议都将不胜感激。

代码语言:javascript
运行
复制
        HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uri);
        webrequest.CookieContainer = cookies;
        webrequest.ContentType = "multipart/form-data; boundary=" + boundary;
        webrequest.Method = "POST";

        // Build up the post message header
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.Append("--"); sb.Append(boundary); sb.Append("\r\n");
        sb.Append("Content-Disposition: form-data; name=\""); sb.Append(fileFormName); 
        sb.Append("\"; filename=\""); sb.Append(Path.GetFileName(uploadfile)); sb.Append("\""); sb.Append("\r\n");
        sb.Append("Content-Type: "); sb.Append("application/octet-stream"); sb.Append("\r\n");
        sb.Append("\r\n");            

        string postHeader = sb.ToString();
        byte[] postHeaderBytes = System.Text.Encoding.UTF8.GetBytes(postHeader);
        // Build the trailing boundary string as a byte array. ensuring the boundary appears on a line by itself
        byte[] boundaryBytes =  System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

        string strRead = "";
        FileStream fileStream = null;
        Stream requestStream = null;
        WebResponse responce = null;
        StreamReader responseStream = null;

        try
        {
            fileStream = new FileStream(uploadfile, FileMode.Open, FileAccess.Read);
            long length = postHeaderBytes.Length + fileStream.Length + boundaryBytes.Length;
            webrequest.ContentLength = length;

            using (requestStream = webrequest.GetRequestStream())
            {
                //requestStream = webrequest.GetRequestStream();
                // Write out our post header
                requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
                // Write out the file contents
                byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)fileStream.Length))];
                int bytesRead = 0;
                while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) { requestStream.Write(buffer, 0, bytesRead); }
                // Write out the trailing boundary
                requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);

                responce = webrequest.GetResponse();
            }
            responseStream = new StreamReader(responce.GetResponseStream());

            strRead = responseStream.ReadToEnd();
        }
        catch (Exception ex)
        {
                     // handle error
        }
        finally
        {
            if( fileStream != null ) fileStream.Close();
            if( requestStream != null ) requestStream.Close();
            if( responseStream != null ) responseStream.Close();
            if( responce != null ) responce.Close();
        }
        return strRead;
EN

回答 1

Stack Overflow用户

发布于 2019-01-10 01:18:33

这是通过在system.webServer中设置maxAllowedContentLength来解决的。Maximum request length exceeded.

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54082154

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档