在写入所有字节之前无法关闭流 [英] Cannot close stream until all bytes are written
本文介绍了在写入所有字节之前无法关闭流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在向 API 服务器发送 POST 请求,并且我重用了之前在其他服务器上成功完成此操作的代码,但出于某种原因,我不知道为什么,它不起作用.我收到错误:
I am sending a POST request to an API server and I have reused code where I have successfully done this before on other servers and for some reason, which I cannot figure out why, it's not working. I get the error:
在写入所有字节之前无法关闭流."
"Cannot close stream until all bytes are written."
即使我正确地声明了内容长度并且我不确定我在这里遗漏了什么......
even though I declared the content length correctly and I am not sure what I am missing here...
data = data + "</posts>"
Dim postBytes As [Byte]() = Encoding.UTF8.GetBytes(data)
Thread.Sleep(10000)
track = data
If uri.Scheme = uri.UriSchemeHttps Then
Dim request As HttpWebRequest = HttpWebRequest.Create(url)
request.Method = "POST"
'//通常我只使用 request.contentlength = postbytes.length 或 data.length
' //normally I just use request.contentlength = postbytes.length or data.length
request.ContentLength = System.Text.Encoding.UTF8.GetByteCount(data)
request.ContentType = "application/xml"
request.KeepAlive = False
request.Timeout = 120000
request.Credentials = New System.Net.NetworkCredential("xxxxxxxxxxxx", "xxxxxxxxx")
Using writer As New StreamWriter(request.GetRequestStream(), Encoding.UTF8)
writer.Write(postBytes)
writer.Flush()
writer.Close()
End Using
Using oResponse As HttpWebResponse = request.GetResponse()
Dim reader As New StreamReader(oResponse.GetResponseStream())
responseData = reader.ReadToEnd()
reader.Close()
oResponse.Close()
End Using
request.Abort()
End If
End If
Catch e As WebException
....
推荐答案
Dim writer As Stream = request.GetRequestStream()
writer.Write(postBytes, 0, postBytes.Length)
writer.Close()
如上更改我的代码
这篇关于在写入所有字节之前无法关闭流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文