【问题标题】:You must write ContentLength bytes to the request stream before calling [Begin]GetResponse您必须在调用 [Begin]GetResponse 之前将 ContentLength 字节写入请求流
【发布时间】:2013-09-06 22:53:47
【问题描述】:

错误: 在调用 [Begin]GetResponse 之前,您必须将 ContentLength 字节写入请求流。

谁能告诉我为什么在运行以下代码时出现上述错误

        Dim xml As New System.Xml.XmlDocument()

        Dim root As XmlElement
        root = xml.CreateElement("root")
        xml.AppendChild(root)

        Dim username As XmlElement
        username = xml.CreateElement("UserName")
        username.InnerText = "xxxxx"
        root.AppendChild(username)

        Dim password As XmlElement
        password = xml.CreateElement("Password")
        password.InnerText = "xxxx"
        root.AppendChild(password)

        Dim shipmenttype As XmlElement
        shipmenttype = xml.CreateElement("ShipmentType")
        shipmenttype.InnerText = "DELIVERY"
        root.AppendChild(shipmenttype)


        Dim url = "xxxxxx"
        Dim req As WebRequest = WebRequest.Create(url)
        req.Method = "POST"
        req.ContentType = "application/xml"
        req.Headers.Add("Custom: API_Method")
        req.ContentLength = xml.InnerXml.Length

        Dim newStream As Stream = req.GetRequestStream()
        xml.Save(newStream)

        Dim response As WebResponse = req.GetResponse()


        Console.Write(response.ToString())

【问题讨论】:

标签: xml vb.net xmlhttprequest webrequest webresponse


【解决方案1】:

简而言之:newStream.Length != xml.InnerXml.Length

  • 首先,XmlDocument.Save(Stream) 将对响应进行编码,这可能导致与.InnerXml 字符串中的字符数不同的字节数。
  • .InnerXML 不一定包含其他内容,例如 XML 序言。

这是一个完整的例子。 (对不起,我的VB有点生疏,所以用C#代替):

using System;
using System.IO;
using System.Net;
using System.Xml;

namespace xmlreq
{
  class Program
  {
    static void Main(string[] args)
    {
      var xml = new XmlDocument();
      var root = xml.CreateElement("root");
      xml.AppendChild(root);

      var req = WebRequest.Create("http://stackoverflow.com/");
      req.Method = "POST";
      req.ContentType = "application/xml";

      using (var ms = new MemoryStream()) {
        xml.Save(ms);
        req.ContentLength = ms.Length;
        ms.WriteTo(req.GetRequestStream());
      }
      Console.WriteLine(req.GetResponse().Headers.ToString());
    }
  }
}

【讨论】:

  • 试过上面的,现在得到这个流不支持seek操作。
  • 那么只需使用 MemoryStream 中介。
  • 请原谅我的无知,但使用 MemoryStream 对您来说是什么意思?
  • 在我的回答中添加了示例(在 C# 中:p)。
  • 感谢代码示例。现在在 req.ContentLength = ms.Length 行上收到此错误
【解决方案2】:

xml.InnerXml 的字符长度可能与xml.Save(newStream) 中实际写入流的字符长度不匹配。例如,检查InnerXml 是否包含 xml 版本节点。另外,我没有看到您指定字符编码,这肯定会影响线路的大小。也许您需要保存到一个临时内存流,获取它的长度,然后在请求中发送它。

【讨论】:

    【解决方案3】:

    我今天遇到了这个错误,问题是端点提供商多年来一直将 http 请求重定向到 https,但改变了他们的策略。所以从

    更新我的代码
    request = WebRequest.Create("http://api.myfaxservice.net/fax.php") 
    

    request = WebRequest.Create("https://api.myfaxservice.net/fax.php")
    

    成功了。如果提供者刚刚关闭了 http,我认为这将是一个更容易运行的问题,因为这个错误让我走上了错误的轨道。

    【讨论】:

      猜你喜欢
      • 2015-09-26
      • 2019-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-07
      • 1970-01-01
      • 2020-06-22
      相关资源
      最近更新 更多