呃,我不断收到 ProtocolViolationException “要写入流的字节超过指定的 Content-Length 字节大小。” 在下面的代码上。
我尝试了多种设置 Content-Length 的方法,但均未成功。
Dim url = "https://domain.com"
Dim req As WebRequest = WebRequest.Create(url)
req.Method = "POST"
req.ContentType = "application/xml"
Dim utf8 As New UTF8Encoding()
req.ContentLength = utf8.GetByteCount(xml.OuterXml)
xml.Save(req.GetRequestStream()) // throws the exception
req.GetRequestStream().Close()
Dim httpResp As WebResponse = req.GetResponse()
Dim stReader As StreamReader = New StreamReader(httpResp.GetResponseStream())
Dim strResponse As String
strResponse = stReader.ReadToEnd()
Console.WriteLine(strResponse)
我尝试使用 xml.OutXML.Length 设置内容长度