码迷,mamicode.com
首页 > Web开发 > 详细

(转)HttpWebRequest以UTF-8编码写入内容时发生“Bytes to be written to the stream exceed the Content-Length bytes size specified.”错误

时间:2014-08-27 14:29:07      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:blog   http   os   使用   ar   art   div   代码   html   

from:http://www.cnblogs.com/Gildor/archive/2010/12/13/1904060.html

 

HttpWebRequest以UTF-8编码写入内容时发生“Bytes to be written to the stream exceed the Content-Length bytes size specified.”错误

出错代码如下:

request.ContentLength = Encoding.UTF8.GetByteCount(content); var writer = new StreamWriter(request.GetRequestStream(), Encoding.UTF8); writer.Write(content); writer.Flush(); writer.Close();

原因是直接使用Encoding.UTF8,在StreamWriter中使用这个Encoding会在流的开头写入byte order mark. 解决方法是改用new UTF8Encoding(false)来构造不写入byte order mark的Encoding.

(转)HttpWebRequest以UTF-8编码写入内容时发生“Bytes to be written to the stream exceed the Content-Length bytes size specified.”错误

标签:blog   http   os   使用   ar   art   div   代码   html   

原文地址:http://www.cnblogs.com/94cool/p/3939311.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!