错误(HttpWebRequest的):字节写入到流超过指定的内容长度的字节大小 [英] Error (HttpWebRequest): Bytes to be written to the stream exceed the Content-Length bytes size specified

查看:4815
本文介绍了错误(HttpWebRequest的):字节写入到流超过指定的内容长度的字节大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法找出为什么我不断收到以下错误:

 字节写入到流超过规定的内容长度的字节大小。 



在以下行:

  writeStream.Write(字节,0,bytes.Length); 

这是一个Windows窗体项目。如果有人知道是怎么回事我一定会感激你。

 私人无效后()
{


HttpWebRequest的要求= NULL;
乌里URI =新的URI(XXXXX);
=请求(HttpWebRequest的)WebRequest.Create(URI);
request.Method =POST;
request.ContentType =应用/的X WWW的形式,进行了urlencoded
XmlDocument的DOC =新的XmlDocument();
doc.Load(XMLFile1.xml);
request.ContentLength = doc.InnerXml.Length;
使用(流writeStream = request.GetRequestStream())
{
UTF8Encoding编码=新UTF8Encoding();
字节[]字节= encoding.GetBytes(doc.InnerXml);
writeStream.Write(字节,0,bytes.Length);
}
字符串结果=的String.Empty;

request.ProtocolVersion = System.Net.HttpVersion.Version11;
request.KeepAlive = FALSE;

{使用
(HttpWebResponse响应=(HttpWebResponse)request.GetResponse())使用
{
(流responseStream = response.GetResponseStream())
{使用
(就是System.IO.StreamReader readStream =新就是System.IO.StreamReader(responseStream,Encoding.UTF8))
{
结果= readStream.ReadToEnd();
}
}
}
}
赶上(例外EXP)
{
// MessageBox.Show(exp.Message);
}
}


解决方案

有有三种可能的选择。




  • 作为的从答案@rene


  • 不要设置CONTENTLENGTH,HttpWebRequest的将是缓冲数据,并设置CONTENTLENGTH自动


  • 设置SendChunked属性为true,并且不设置CONTENTLENGTH。该请求被发送块编码到Web服务器。 (需要HTTP 1.1,并通过网络服务器来支持)




代码:

  ... 
request.SendChunked = TRUE;
使用(流writeStream = request.GetRequestStream())
{...}


I can't seem to figure out why I keep getting the following error:

Bytes to be written to the stream exceed the Content-Length bytes size specified.

at the following line:

writeStream.Write(bytes, 0, bytes.Length);

This is on a Windows Forms project. If anyone knows what is going on here I would surely owe you one.

    private void Post()
    {


        HttpWebRequest request = null;
        Uri uri = new Uri("xxxxx");
        request = (HttpWebRequest)WebRequest.Create(uri);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        XmlDocument doc = new XmlDocument();
        doc.Load("XMLFile1.xml");
        request.ContentLength = doc.InnerXml.Length;
        using (Stream writeStream = request.GetRequestStream())
        {
            UTF8Encoding encoding = new UTF8Encoding();
            byte[] bytes = encoding.GetBytes(doc.InnerXml);
            writeStream.Write(bytes, 0, bytes.Length);
        }
        string result = string.Empty;

        request.ProtocolVersion = System.Net.HttpVersion.Version11;
        request.KeepAlive = false;
        try
        {
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (System.IO.StreamReader readStream = new System.IO.StreamReader(responseStream, Encoding.UTF8))
                    {
                        result = readStream.ReadToEnd();
                    }
                }
            }
        }
        catch (Exception exp)
        {
            // MessageBox.Show(exp.Message);
        }
    }

解决方案

There are three possible options

  • Fix the ContentLength as described in the answer from @rene

  • Don't set the ContentLength, the HttpWebRequest is buffering the data, and sets the ContentLength automatically

  • Set the SendChunked property to true, and don't set the ContentLength. The request is send chunk encoded to the webserver. (needs HTTP 1.1 and has to be supported by the webserver)

Code:

...
request.SendChunked = true;
using (Stream writeStream = request.GetRequestStream())
{ ... }

这篇关于错误(HttpWebRequest的):字节写入到流超过指定的内容长度的字节大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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