是否可以从URL读取到System.IO.Stream对象?

时间:2009-08-03 16:34:32

标签: .net vb.net io iostream

我试图从url读取System.IO.Stream对象。我试着用

Dim stream as Stream = New FileStream(msgURL, FileMode.Open)

但是我收到一个错误,即FileStream对象不支持URI格式。是否有一些我可以使用的方法继承自能够从URL读取的System.IO.Stream?

3 个答案:

答案 0 :(得分:32)

使用WebClient.OpenRead

Using wc As New WebClient()
    Using stream As Stream = wc.OpenRead(msgURL)
        ...
    End Using
End Using

答案 1 :(得分:19)

VB.Net:

Dim req As WebRequest = HttpWebRequest.Create("url here")
Using stream As Stream = req.GetResponse().GetResponseStream()

End Using

C#:

var req = System.Net.WebRequest.Create("url here");
using (Stream stream = req.GetResponse().GetResponseStream())
{

}

答案 2 :(得分:2)

是的,您可以使用HttpWebRequest对象来获取响应流:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
Stream receiveStream = response.GetResponseStream();
// read the stream
receiveStream.Close();
response.Close();

(从the docs中删除并简化。)