我正在
这是我的代码:
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会为该长度抛出此异常:
是什么原因造成的?
最佳答案
最终通过使用以下方法工作:
using (dataStream = Webrequest.GetRequestStream())
{
dataStream.Write(byteArray, 0, byteArray.Length);
}
代替:
dataStream = Webrequest.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
关于c# - System.Net.ProtocolViolationException:必须在调用[Begin] GetResponse之前将ContentLength字节写入请求流,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31297612/