首页 > 代码库 > (转)HttpWebRequest以UTF-8编码写入内容时发生“Bytes to be written to the stream exceed the Content-Length bytes size specified.”错误
(转)HttpWebRequest以UTF-8编码写入内容时发生“Bytes to be written to the stream exceed the Content-Length bytes size specified.”错误
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.”错误
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。