C# 读取网页内容 Streamreader

发布于 2024-11-19 18:48:24 字数 456 浏览 1 评论 0原文

我需要在 Streamreader 中读取网页的内容,例如

www.example.com

<test>
<sample></sample>
</test>

我得到了这个:

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

但是我得到了这个错误代码

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

I need to read the content of a webpage in streamreader like

www.example.com

<test>
<sample></sample>
</test>

i got this:

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

but i then i get this error code

Attempt to access the method failed:
System.IO.StreamReader..ctor(System.String)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

马蹄踏│碎落叶 2024-11-26 18:48:24

尝试一下 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
}

Try a WebClient, it's easier and you don't have to worry about streams and rivers:

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
}
春庭雪 2024-11-26 18:48:24

如果您想使用 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;
    }

If you want to use the StreamReader, here is the code I am using:

    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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文