【发布时间】:2015-09-26 15:43:49
【问题描述】:
我收到了
“System.Net.ProtocolViolationException: You must write ContentLength bytes to the request stream before calling [Begin]GetResponse” 调用 Web 请求的“BeginGetResponse”方法时出错。
这是我的代码:
try
{
Stream dataStream = null;
WebRequest Webrequest;
Webrequest = WebRequest.Create(this.EndPointAddress);
Webrequest.Credentials = new NetworkCredential(this.username, this.password);
Webrequest.ContentType = "text/xml";
Webrequest.Method = WebRequestMethods.Http.Post;
byteArray = System.Text.Encoding.UTF8.GetBytes(xmlRequest.Children[0].InnerXML);
Webrequest.ContentLength = byteArray.Length;
dataStream = Webrequest.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
RequestState rs = new RequestState();
rs.Request = Webrequest;
IAsyncResult r = (IAsyncResult)Webrequest.BeginGetResponse(new AsyncCallback(RespCallback), rs);
}
catch (Exception exc)
{
TRACE.EXCEPTION(exc);
}
finally
{
dataStream.Close();
}
更具体地说,在调用函数“getRequestStream()”之后,Stream 会抛出这个异常:
“stream.Length”引发了“System.NotSupportedException”类型的异常
可能是什么原因造成的?
【问题讨论】:
-
请不要在您的帖子中添加“搜索了很多”和“谢谢”。为了显示努力 - 发布代码(就像在当前帖子中一样)和指向您尝试过的解决方案的链接以及没有解决您问题的短句。
-
该问题解释的错误与此问题不同,因此不会重复。
标签: c# exception webrequest content-length