HttpWebRequest以UTF-8编码写入内容时发生“Bytes to be written to the stream exceed the Content-Length bytes size specified.”错误
2010-12-13 01:10
731 查看
出错代码如下:
原因是直接使用Encoding.UTF8,在StreamWriter中使用这个Encoding会在流的开头写入byte order mark. 解决方法是改用new UTF8Encoding(false)来构造不写入byte order mark的Encoding.
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.
- cannot be cast to org.springframework.web.multipart.MultipartHttpServletRequest
- php错误:failed to open stream: HTTP request failed!
- 新建flex Library Project项目错误 Nothing Was Specified to be included in the library
- org.apache.shiro.web.servlet.ShiroHttpServletRequest cannot be cast to org.springframwork.web.mult..
- httpwebresponse 异步: request.BeginGetRequestStream 报错! 无法发送具有此谓词类型的内容正文。
- .ShiroHttpServletRequest cannot be cast to org.springframework.web.multipart.MultipartHttpServletReq
- WCF分布式开发常见错误(23):This could be due to the fact that the server certificate is not configured properly with HTTP.SYS
- java.lang.ClassCastException: org.apache.shiro.web.servlet.ShiroHttpServletRequest cannot be cast to
- php错误提示failed to open stream: HTTP request failed!的完美解决方法
- Silverlight使用HttpWebRequest和HttpWebResponse时发生System.Security.SecurityException: 安全性错误
- C#利用HttpWebRequest后台请求,出现:基础连接已经关闭: 接收时发生错误;【解决方法之一】
- 新建flex Library Project项目错误 Nothing Was Specified to be included in the library
- httpwebresponse 异步: request.BeginGetRequestStream 报错! 无法发送具有此谓词类型的内容正文。
- org.apache.shiro.web.servlet.ShiroHttpServletRequest cannot be cast to org.springframwork.web.mult..
- cannot be cast to org.springframework.web.multipart.MultipartHttpServletRequest