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
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()
I found the problem. The service wasn't reterning anything so the stream was empty.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.