locked
StreamWriter Error: Cannot close stream until all bytes are written. RRS feed

  • Question

  • User-1836216065 posted

     Hi,

     I have a testing platform that allows data to be posted to a real time collection system.  It uses the StreamWriter object to send both HTTP and XML formats,

     It works fine, but there is a new request to submit uploaded files.  This again works fine, unless the files are over 70k (we need to test upto 3MB).  If it is over 70k, the response is the content-length too big

     I have changed it so that the content-length is now set dynamically, dependant on the size of the file uploaded.  However, when the StreamWriter.Close() runs, the execption The request was aborted: The request was canceled. is generated, with the InnerException Cannot close stream until all bytes are written.

    From what I have found on the web, the solution is use StreamWriter.Flush(), but this code is already in place.

     Code is as follows: 

            With WebRequest
    .Method = "POST"
    .ContentType = "application/x-www-form-urlencoded"
    .PreAuthenticate = False
    .AuthenticationLevel = Security.AuthenticationLevel.None
    End With

    If
    FileSize > 0 Then
    WebRequest.ContentLength = FileSize
    End If

    Try

    RequestStream = WebRequest.GetRequestStream()
    StreamWriter = New StreamWriter(RequestStream)
    StreamWriter.Write(strDataToPost)
    StreamWriter.Flush()
    RequestStream.Flush()
    StreamWriter.Close()
    RequestStream.Close()
    Catch ex As Exception
    RTResponse(0) = -1
    RTResponse(1) = ex.Message
    End Try

     I have also tried amending the timeout, but it makes no difference.

     Any help would be greatly appreciated.

    Tuesday, February 10, 2009 7:14 AM

Answers

  • User-990694832 posted

    A person could try streamwriter.dispose

    Also, naming the variables the same name as the class could be causeing the error 

    use strWriter = new streamWriter 

    Hope this helps

    DK

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 10, 2009 10:14 AM

All replies

  • User-990694832 posted

    A person could try streamwriter.dispose

    Also, naming the variables the same name as the class could be causeing the error 

    use strWriter = new streamWriter 

    Hope this helps

    DK

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 10, 2009 10:14 AM
  • User1941953360 posted

    Increase the value of "HttpWebRequest.Timeout". This value restricts the total time within which the reqest must complete.

    Thursday, September 2, 2010 5:41 AM