C#阅读网页内容Streamreader

我需要在streamreader中阅读网页内容

www.example.com

   

我懂了:

 System.IO.StreamReader StreamReader1 = new System.IO.StreamReader("www.example.com"); string test = StreamReader1.ReadToEnd(); 

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

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

尝试使用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 } 

如果你想使用StreamReader,这里是我正在使用的代码:

  const int Buffer_Size = 100 * 1024; WebRequest request = CreateWebRequest(uri); WebResponse response = request.GetResponse(); result = GetPageHtml(response); 

  private string GetPageHtml(WebResponse response) { char[] buffer = new char[Buffer_Size]; Stream responseStream = response.GetResponseStream(); using(StreamReader reader = new StreamReader(responseStream)) { int index = 0; int readByte = 0; do { readByte = reader.Read(buffer, index, 256); index += readByte; } while (readByte != 0); response.Close(); } string result = new string(buffer); result = result.TrimEnd(new char[] {'\0'}); return result; }