System.Net.ProtocolViolationException:您必须在调用 [Begin]GetResponse 之前将 ContentLength 字节写入请求流
System.Net.ProtocolViolationException: You must write ContentLength bytes to the request stream before calling [Begin]GetResponse
我得到
"System.Net.ProtocolViolationException: You must write ContentLength bytes to the request stream before calling [Begin]GetResponse" error when calling to the "BeginGetResponse" method of the web request.
这是我的代码:
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' threw an exception of type 'System.NotSupportedException'
可能是什么原因造成的?
这终于通过使用实现了:
using (dataStream = Webrequest.GetRequestStream())
{
dataStream.Write(byteArray, 0, byteArray.Length);
}
而不是:
dataStream = Webrequest.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
您的代码应该适用于 .NET 2.0 从 4.0 开始您应该在编写后关闭流:
dataStream = Webrequest.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
datastream.Close();
检查以确认您的服务器设置为接受大文件。您可能会发现您 运行 进入了 4 兆的默认限制。
将以下内容添加到您的 web.config 文件以上传更大的文件:
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="104857600" />
</requestFiltering>
</security>
</system.webServer>
我得到
"System.Net.ProtocolViolationException: You must write ContentLength bytes to the request stream before calling [Begin]GetResponse" error when calling to the "BeginGetResponse" method of the web request.
这是我的代码:
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' threw an exception of type 'System.NotSupportedException'
可能是什么原因造成的?
这终于通过使用实现了:
using (dataStream = Webrequest.GetRequestStream())
{
dataStream.Write(byteArray, 0, byteArray.Length);
}
而不是:
dataStream = Webrequest.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
您的代码应该适用于 .NET 2.0 从 4.0 开始您应该在编写后关闭流:
dataStream = Webrequest.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
datastream.Close();
检查以确认您的服务器设置为接受大文件。您可能会发现您 运行 进入了 4 兆的默认限制。
将以下内容添加到您的 web.config 文件以上传更大的文件:
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="104857600" />
</requestFiltering>
</security>
</system.webServer>