C#阅读网页内容Streamreader

Lar*_*man 5 c# url streamreader web

我需要在streamreader中读取网页的内容

www.example.com

<test>
<sample></sample>
</test>
Run Code Online (Sandbox Code Playgroud)

我懂了:

System.IO.StreamReader StreamReader1 =
new System.IO.StreamReader("www.example.com");
string test = StreamReader1.ReadToEnd();
Run Code Online (Sandbox Code Playgroud)

但我然后我得到这个错误代码

尝试访问该方法失败:System.IO.StreamReader..ctor(System.String)

Dar*_*rov 27

尝试使用WebClient,它更容易,您不必担心流和河流:

using (var client = new WebClient())
{
    string result = client.DownloadString("http://www.example.com");
    // TODO: do something with the downloaded result from the remote
    // web site
}
Run Code Online (Sandbox Code Playgroud)