I get error when trying to write response stream to a file

Posted by MemphisDeveloper on Stack Overflow See other posts from Stack Overflow or by MemphisDeveloper
Published on 2010-05-28T15:00:23Z Indexed on 2010/05/28 16:51 UTC
Read the original article Hit count: 294

Filed under:
|
|
|
|

I am trying to test a rest webservice but when I do a post and try to retreive the save the response stream to a file I get an exception saying "Stream was not readable." What am I doing wrong?

Public Sub PostAndRead()
    Dim flReader As FileStream = New FileStream("~\testRequest.xml", FileMode.Open, FileAccess.Read)
    Dim flWriter As FileStream = New FileStream("~\testResponse.xml", FileMode.Create, FileAccess.Write)
    Dim address As Uri = New Uri(restAddress)
    Dim req As HttpWebRequest = DirectCast(WebRequest.Create(address), HttpWebRequest)

    req.Method = "POST"
    req.ContentLength = flReader.Length
    req.AllowWriteStreamBuffering = True

    Dim reqStream As Stream = req.GetRequestStream()

    ' Get data from upload file to inData  
    Dim inData(flReader.Length) As Byte
    flReader.Read(inData, 0, flReader.Length)

    ' put data into request stream 
    reqStream.Write(inData, 0, flReader.Length)
    flReader.Close()
    reqStream.Close()

    ' Post Response
    req.GetResponse()

    ' Save results in a file
    Copy(req.GetRequestStream(), flWriter)
End Sub

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about vb.net