【发布时间】: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())
【问题讨论】:
-
也许这有帮助:forums.asp.net/t/…
标签: xml vb.net xmlhttprequest webrequest webresponse