Vb.Net - 写入的字节超过 XML Post 中的内容长度字节
呃,我不断收到 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 设置内容长度
Ugh, I keep getting a ProtocolViolationException "Bytes to be written to the stream exceed the Content-Length bytes size specified." on the following code.
I've tried setting Content-Length numerous ways with no success.
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)
I've tried setting the content-length using xml.OutXML.Length
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用 WebClient,它使代码更容易它负责正确地冲洗和处理所有流:
Try with a WebClient, it makes the code easier and it takes care of properly flushing and disposing all the streams: