错误:您必须在调用 [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())