(转)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.”错误

出错代码如下:

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.




本文转自94cool博客园博客,原文链接:http://www.cnblogs.com/94cool/p/3939311.html,如需转载请自行联系原作者

相关文章
|
5月前
|
编解码 开发者 Python
详解'unicodeescape' codec can't decode bytes in position 16-17: malformed \N character escape
遇到“`unicodeescape` codec can't decode bytes in position X-X: malformed \N character escape”错误时,首先不要慌张。通过逐步检查和修正代码中的Unicode转义序列,大多数情况下都能找到问题的根源并加以解决。有效利用Python的Unicode支持特性,可以优雅地处理各种复杂的字符编码问题。
133 3
|
5月前
|
编解码
UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xe9 in position 3114: invalid continuation byte
UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xe9 in position 3114: invalid continuation byte
|
编解码 Python
pandas - read_csv报错:‘utf-8‘/‘gbk‘ codec can‘t decode byte 0xb1 in position 0:invalid start byte
pandas - read_csv报错:‘utf-8‘/‘gbk‘ codec can‘t decode byte 0xb1 in position 0:invalid start byte
421 0
成功解决UnicodeDecodeError: 'utf-8' codec can't decode byte 0xce in position 130: invalid continuation b
成功解决UnicodeDecodeError: 'utf-8' codec can't decode byte 0xce in position 130: invalid continuation b
成功解决UnicodeDecodeError: 'utf-8' codec can't decode byte 0xce in position 130: invalid continuation b
|
存储 编解码 JavaScript
UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xb0 in position 53: invalid start byte
UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xb0 in position 53: invalid start byte
236 0
UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xb0 in position 53: invalid start byte
|
编解码 Python
pandas读取csv错误UnicodeDecodeError: 'utf-8' codec can't decode byte 0xba in position 0: invalid start byte
pandas读取csv错误UnicodeDecodeError: 'utf-8' codec can't decode byte 0xba in position 0: invalid start byte
682 0
|
编解码 Python
问题解决:UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xcf in position 0: invalid continuation by
问题解决:UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xcf in position 0: invalid continuation by
3656 0
问题解决:UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xcf in position 0: invalid continuation by
|
编解码 Python
Python ‘utf-8‘ codec can‘t decode byte 0x8b in position 1: invalid start byte
Python ‘utf-8‘ codec can‘t decode byte 0x8b in position 1: invalid start byte
217 0
|
编解码
解码错误。‘gb2312‘ codec can‘t decode byte 0xf3 in position 307307: illegal multibyte sequence
解码错误。‘gb2312‘ codec can‘t decode byte 0xf3 in position 307307: illegal multibyte sequence
151 0
|
应用服务中间件 nginx Windows
1113: No mapping for the Unicode character exists in the target multi-byte code page
1113: No mapping for the Unicode character exists in the target multi-byte code page
741 0
1113: No mapping for the Unicode character exists in the target multi-byte code page