Results 1 to 3 of 3

Thread: Cannot close stream until all bytes are written."

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Location
    USA
    Posts
    257

    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 --

  2. #2
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    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

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Location
    USA
    Posts
    257

    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
  •  



Click Here to Expand Forum to Full Width