vb.net 在调用 [Begin]GetResponse 之前,您必须将 ContentLength 字节写入请求流
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18596788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
You must write ContentLength bytes to the request stream before calling [Begin]GetResponse
提问by user2516387
Error: You must write ContentLength bytes to the request stream before calling [Begin]GetResponse.
错误:在调用 [Begin]GetResponse 之前,您必须将 ContentLength 字节写入请求流。
Can anyone advise why I am getting the above error when running the following code
谁能告诉我为什么在运行以下代码时出现上述错误
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())
采纳答案by tcarvin
Probably a length mismatch between the character length of xml.InnerXmland what is actually written to the stream in xml.Save(newStream). Check if InnerXmlincludes the xml version node, for example. Also, I don't see you specifying a character encoding, which definitely affects the size on the wire. Perhaps you need to save to a temporary memory stream, get the length of that, and then send that in the request.
可能是字符长度xml.InnerXml与实际写入流中的字符长度不匹配xml.Save(newStream)。例如,检查是否InnerXml包含 xml 版本节点。另外,我没有看到您指定了字符编码,这肯定会影响线路上的大小。也许您需要保存到临时内存流,获取它的长度,然后在请求中发送它。
回答by nmaier
In short: newStream.Length != xml.InnerXml.Length.
简而言之:newStream.Length != xml.InnerXml.Length。
- First of all,
XmlDocument.Save(Stream)will encode the response, which may result in a different number of bytes than are chars in the.InnerXmlstring. .InnerXMLdoes not necessarily contain other stuff, such as the XML preamble.
- 首先,
XmlDocument.Save(Stream)将对响应进行编码,这可能会导致字节数与.InnerXml字符串中的字符数不同。 .InnerXML不一定包含其他内容,例如 XML 序言。
Here is a complete example. (Sorry, my VB is a bit rusty, so C# instead):
这是一个完整的例子。(抱歉,我的 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());
}
}
}
回答by TonyE
I had this error today and the issue was that the endpoint provider had been redirecting http requests to https for years, but changed their policy. So updating my code from
我今天遇到了这个错误,问题是端点提供商多年来一直将 http 请求重定向到 https,但更改了他们的策略。所以更新我的代码
request = WebRequest.Create("http://api.myfaxservice.net/fax.php")
to
到
request = WebRequest.Create("https://api.myfaxservice.net/fax.php")
did the trick. If the provider would have just shut off http, I think it would have been an easier problem to run down as this error was putting me on the wrong track.
成功了。如果提供者刚刚关闭了 http,我认为这将是一个更容易解决的问题,因为这个错误让我走上了错误的轨道。

