发送POST请求,报错:写入流的字节超出指定的 Content-Length 字节大小

zf12102926 2017-01-19 09:14:32

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";

request.ContentLength = Encoding.UTF8.GetBytes(postDataStr).Length;
StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.GetEncoding("utf-8"));
writer.Write(postDataStr);
writer.Flush();


代码如上,因为post数据中有中文,所以用utf-8编码,但是报错:System.Net.ProtocolViolationException: 写入流的字节超出指定的 Content-Length 字节大小。

求教如何修改才正确
...全文
2695 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_41150952 2019-03-27
  • 打赏
  • 举报
回复
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:8000/api/token-auth/"); request.Method = "POST"; request.KeepAlive = false; string postData = "username=20153916&password=********&passTicket=********"; UTF8Encoding encoding = new UTF8Encoding(); byte[] byte1 = encoding.GetBytes(postData); // Set the content type of the data being posted. request.ContentType = "application/x-www-form-urlencoded"; // Set the content length of the string being posted. request.ContentLength = byte1.Length; Stream newStream = request.GetRequestStream(); newStream.Write(byte1, 0, byte1.Length); Console.WriteLine("The value of 'ContentLength' property after sending the data is {0}", request.ContentLength); // Close the Stream object. newStream.Close(); 用这个写法就没错,不知道为什么
明如正午 2018-03-22
  • 打赏
  • 举报
回复
引用 3 楼 zf12102926 的回复:
我做的是对微信进行自定义菜单的设置,需要post一段json过去。 情况一:我把utf-8换成ASCII,微信菜单能成功生成,但是中文会显示成乱码,当然,用ASCII,中文肯定是乱码; 情况二:用utf-8编码,不指定ContentLength,然后调试了一下,post请求成功发送,但是微信返回错误,40016错误,此错误的说明是"不合法的按钮个数",而我的json在微信官网的调试工具下是可以的,所以应该不是json的问题; 于是我再网上又找了一段发post请求的代码:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";

byte[] postBytes = Encoding.UTF8.GetBytes(postDataStr);
request.ContentLength = Encoding.UTF8.GetBytes(postDataStr).Length;
using (Stream reqStream = request.GetRequestStream()) {
      reqStream.Write(postBytes, 0, postBytes.Length); 
}
这次就成功了,post请求成功发送,菜单也成功生成,并且中文正常显示。 我又在这一段新的代码上,尝试不设置ContentLength,并加上Flush,结果仍旧是成功的,可能是因为我把StreamWriter换成Stream的缘故?
这个真的有用,解决了我的问题!感谢
zf12102926 2017-01-19
  • 打赏
  • 举报
回复
我做的是对微信进行自定义菜单的设置,需要post一段json过去。 情况一:我把utf-8换成ASCII,微信菜单能成功生成,但是中文会显示成乱码,当然,用ASCII,中文肯定是乱码; 情况二:用utf-8编码,不指定ContentLength,然后调试了一下,post请求成功发送,但是微信返回错误,40016错误,此错误的说明是"不合法的按钮个数",而我的json在微信官网的调试工具下是可以的,所以应该不是json的问题; 于是我再网上又找了一段发post请求的代码:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";

byte[] postBytes = Encoding.UTF8.GetBytes(postDataStr);
request.ContentLength = Encoding.UTF8.GetBytes(postDataStr).Length;
using (Stream reqStream = request.GetRequestStream()) {
      reqStream.Write(postBytes, 0, postBytes.Length); 
}
这次就成功了,post请求成功发送,菜单也成功生成,并且中文正常显示。 我又在这一段新的代码上,尝试不设置ContentLength,并加上Flush,结果仍旧是成功的,可能是因为我把StreamWriter换成Stream的缘故?
DragonerHuang 2017-01-19
  • 打赏
  • 举报
回复
<requestFiltering> <requestLimits maxAllowedContentLength=""></requestLimits> </requestFiltering> 你设置最大值可以了
  • 打赏
  • 举报
回复 1
你不要指定ContentLength就行了

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧