views:

105

answers:

2

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
+1  A: 

You have closed request stream

reqStream.Close()

Then in this statement request stream is not longer available

Copy(req.GetRequestStream(), flWriter)

Try to move close to the end

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

' Post Response
req.GetResponse()

' Save results in a file
Copy(req.GetRequestStream(), flWriter)
reqStream.Close()
volody
it continued to throw the same message. Is there a way to manually post an xml to a webservice using a browser something like:POST http://url/Endpoint <xml file>
MemphisDeveloper
A: 

I found the problem. The service wasn't reterning anything so the stream was empty.

MemphisDeveloper