Link to home
Start Free TrialLog in
Avatar of DEWebWorks
DEWebWorks

asked on

System.IO.IOException: Cannot close stream until all bytes are written when sending webrequest

I am trying to send a web request but am getting the following error:
"System.IO.IOException: Cannot close stream until all bytes are written."
on the following line
myWriter.Close()

Here is my code snippet

objRequest = CType(WebRequest.Create(post_url), HttpWebRequest)
        objRequest.Method = "POST"
        objRequest.ContentLength = 54
        objRequest.ContentType = "application/x-www-form-urlencoded"
        objRequest.Headers("Authorization") = "BASIC " & authorization_string

        post_string = "&command=requestnewpassword&application=netconnect"

        ' post data is sent as a stream
        myWriter = Nothing
        myWriter = New StreamWriter(objRequest.GetRequestStream())
        myWriter.Write(post_string)
        myWriter.Close()

Anyone have any ideas how to fix this?
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Try calling the Flush before close.
Maybe u could add a try and call the close method in finally block
Avatar of DEWebWorks
DEWebWorks

ASKER

I tried adding myWriter.Flush() just before the close line, but it didn't change anything.
ASKER CERTIFIED SOLUTION
Avatar of DEWebWorks
DEWebWorks

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I found the answer elsewhere, but wanted to show the answer for others that might encounter this same problem.