怎样下载网页成.NET流 [英] How do I download a webpage into a stream in .NET
问题描述
我知道这应该是一个基本的问题,但我打砖墙。 我期待去一个URL / URI下载得到的字符串,如果我打开一个文件,然后把它弄出来到字符串变量。
我一直在馅大约有IO.Stream和Net.httpxxx,但还没有设法让元素排队以正确的方式。
我得到不支持给定路径的格式从标准流打开该网页,因为它不是在本地文件系统......这一点我明白了,有点我不明白的是...我怎么做到的equivelent:
公共功能GetWebPageAsString(的pURL作为字符串)作为字符串
昏暗lStream作为IO.StreamReader =新就是System.IO.StreamReader(的pURL)
返回lStream.ReadToEnd
端功能
简短的回答,在C#中,看起来像
使用(System.Net.WebClient客户端=新System.Net.WebClient())
{
字符串的HTML = client.DownloadString(http://www.google.com);
}
I know this should be a basic question but I am hitting a brick wall. I am looking to go to a URL/URI download the resulting string as if I had opened a file and then get it out into a String variable.
I have been stuffing about with IO.Stream and Net.httpxxx but haven't managed to get the elements to line up in the right way.
I get "the given path's format is not supported" from opening the page in the standard stream, because it's not in the local file system ... that bit i understand, the bit I don't get is ... how do I achieve the equivelent of:
Public Function GetWebPageAsString(pURL As String) As String
Dim lStream As IO.StreamReader = New System.IO.StreamReader(pURL)
Return lStream.ReadToEnd
End Function
The short answer, in C#, looks like
using(System.Net.WebClient client = new System.Net.WebClient())
{
string html = client.DownloadString("http://www.google.com");
}
这篇关于怎样下载网页成.NET流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!