尝试写入响应流文件时,我得到错误

我想测试一个休息web服务,但是当我做了后,并尝试中检索保存的响应流中,我得到一个例外,说文件“流是无法读取。” 我究竟做错了什么?

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

--------------解决方案-------------

已关闭的请求流

reqStream.Close()

然后,在这个声明中请求流不再可用

Copy(req.GetRequestStream(), flWriter)

尝试移动接近尾声

' 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()

我发现这个问题。 该服务没有reterning过这么流是空的。

分类:asp.net 时间:2015-03-15 人气:0
分享到:

相关文章

Copyright (C) 55228885.com, All Rights Reserved.

55228885 版权所有 京ICP备15002868号

processed in 0.747 (s). 10 q(s)