|
-
Dec 14th, 2010, 11:17 AM
#1
Thread Starter
Hyperactive Member
Cannot close stream until all bytes are written."
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...
Code:
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"
' 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
....
Any ideas please...
-- Please rate me if I am helpful --
-
Dec 14th, 2010, 11:30 AM
#2
Re: Cannot close stream until all bytes are written."
Interesting, I did find this thread about UTF8 characters causing issues with .Write: http://social.msdn.microsoft.com/for...1-432c8af2c369
-
Dec 14th, 2010, 11:49 AM
#3
Thread Starter
Hyperactive Member
Re: Cannot close stream until all bytes are written."
Thanks for your response, I figured it out and changed my code a little;
Code:
Dim writer As Stream = request.GetRequestStream()
writer.Write(postBytes, 0, postBytes.Length)
writer.Close()
-- Please rate me if I am helpful --
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|