【问题标题】:Either ContentLength must be set to a non-negative number, or SendChunked set to true in order to perform the write operationContentLength 必须设置为非负数,或者 SendChunked 设置为 true 才能执行写入操作
【发布时间】:2019-09-05 13:40:53
【问题描述】:

使用HttpWebRequest,我使用以下代码请求soap并获取流:

XmlDocument soapEnvelopeXml = new XmlDocument();
HttpWebRequest webRequest = ...

using (Stream stream = webRequest.GetRequestStream())
{
    soapEnvelopeXml.Save(stream);
}

虽然这是错误消息:

Either ContentLength must be set to a non-negative number, or SendChunked set to true in order to perform the write operation when AllowWriteStreamBuffering is disabled.

正如上面的消息所说,我无法写入流,因为如果我调用 save soapEvelopeXml 应用程序将结束。

【问题讨论】:

    标签: c# soap stream httpwebrequest xmldocument


    【解决方案1】:

    要解决错误,只需在 HttpWebRequest 对象中启用一个参数即可。

    webRequest.AllowWriteStreamBuffering = true;
    

    该参数是对象属性的一部分,引用自 docs.microsoft.com 上的文档

    AllowWriteStreamBuffering : 获取或设置一个值,该值指示是否对发送到 Internet 资源的数据进行缓冲

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-14
      • 2021-11-08
      • 2012-06-09
      • 1970-01-01
      • 1970-01-01
      • 2019-01-15
      • 2011-01-19
      • 1970-01-01
      相关资源
      最近更新 更多