本文介绍了System.Net.ProtocolViolationException:您必须调用[开始]之前写CONTENTLENGTH字节请求流的GetResponse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我收到
System.Net.ProtocolViolationException:您必须编写CONTENTLENGTH字节来请求流之前调用[开始] 。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()之后,流抛出此异常的长度:
More specifically, after calling to the function "getRequestStream()", the Stream is throwing this exception for the lenght:
stream.Length'扔类型的异常的System.NotSupportedException
什么引起的?
推荐答案
这
using (dataStream = Webrequest.GetRequestStream())
{
dataStream.Write(byteArray, 0, byteArray.Length);
}
而不是:
Instead of:
dataStream = Webrequest.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
这篇关于System.Net.ProtocolViolationException:您必须调用[开始]之前写CONTENTLENGTH字节请求流的GetResponse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!