如何将网站内容下载到字符串? [英] How can i download a website content to a string?

查看:40
本文介绍了如何将网站内容下载到字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过此操作,并且希望将网站的源内容下载到字符串中:

I tried this and i want that the source content of the website will be download to a string:

public partial class Form1 : Form
    {
        WebClient client;
        string url;
        string[] Search(string SearchParameter);


        public Form1()
        {
            InitializeComponent();

            url = "http://chatroll.com/rotternet";
            client = new WebClient();




            webBrowser1.Navigate("http://chatroll.com/rotternet");
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        static void DownloadDataCompleted(object sender,
           DownloadDataCompletedEventArgs e)
        {



        }


        public string SearchForText(string SearchParameter)
        {
            client.DownloadDataCompleted += DownloadDataCompleted;
            client.DownloadDataAsync(new Uri(url));
            return SearchParameter;
        }

我想使用WebClient和downloaddataasync,最后将网站源内容包含在字符串中.

I want to use WebClient and downloaddataasync and in the end to have the website source content in a string.

推荐答案

使用 WebRequest :

WebRequest request = WebRequest.Create(url);
request.Method = "GET";
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
string content = reader.ReadToEnd();
reader.Close();
response.Close();

您可以轻松地从另一个线程中调用代码,也可以使用后台操作-这将使您的UI在检索数据时具有响应性.

You can easily call the code from within another thread, or use background worer - that will make your UI responsive while retrieving data.

这篇关于如何将网站内容下载到字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆