(转)HttpWebRequest以UTF-8编码写入内容时发生“Bytes to be written to the stream exceed the Content-Length bytes size specified.”错误
2014-08-27 13:59
831 查看
from:http://www.cnblogs.com/Gildor/archive/2010/12/13/1904060.html
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.”错误
出错代码如下: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.”错误
- Bytes to be written to the stream exceed the Content-Length bytes size specified 解决方法
- https使用HttpWebRequest出现错误:System.IO.IOException: Received an unexpected EOF or 0 bytes from the transport stream.的解决方法
- android WebView加载不出网页内容This request has been blocked; the content must be served over HTTPS.
- android WebView加载不出网页内容This request has been blocked; the content must be served over HTTPS.
- How to use the HttpWebRequest object
- php错误提示failed to open stream: HTTP request failed!的完美解决方法
- git错误error: Your local changes to the following files would be overwritten
- (微信API接口开发) 使用HttpWebRequest进行请求时发生错误:基础连接已关闭,发送时发生错误处理
- httpwebresponse 异步: request.BeginGetRequestStream 报错! 无法发送具有此谓词类型的内容正文。
- 使用HttpWebRequest进行请求时发生错误:基础连接已关闭,发送时发生错误处理
- 解决HttpWebRequest写入数据“操作超时”错误
- All bytes must be within the maximum size specified by CreateFileMapping
- WCF分布式开发常见错误(23):This could be due to the fact that the server certificate is not configured properly with HTTP.SYS
- php错误:failed to open stream: HTTP request failed!
- eclipse里的错误提示为The import javax.servlet.http.HttpServletRequest cannot be resolved
- 服务器端安装ASP.NET运行环境错误Failed to execute request because the App-Domain could not be created. Error: 0x80131902
- php错误提示failed to open stream: HTTP request failed!的完美解决方法
- 新建flex Library Project项目错误 Nothing Was Specified to be included in the library
- cannot be cast to org.springframework.web.multipart.MultipartHttpServletRequest