WebRequest 多个页面并加载到 StreamReader 中

发布于 2024-12-08 21:44:05 字数 1943 浏览 0 评论 0原文

我想使用 ASP.NET 4.0 转到多个页面,复制所有 HTML,然后最后将其粘贴到文本框中。从那里我想运行我的解析函数,处理这个问题的最佳方法是什么?

 protected void goButton_Click(object sender, EventArgs e)
    {
        if (datacenterCombo.Text == "BL2")
        {
            fwURL = "http://website1.com/index.html";
            l2URL = "http://website2.com/index.html";
            lbURL = "http://website3.com/index.html";
            l3URL = "http://website4.com/index.html";
            coreURL = "http://website5.com/index.html";

            WebRequest objRequest = HttpWebRequest.Create(fwURL);
            WebRequest layer2 = HttpWebRequest.Create(l2URL);

            objRequest.Credentials = CredentialCache.DefaultCredentials;
            using (StreamReader layer2 = new StreamReader(layer2.GetResponse().GetResponseStream()))


            using (StreamReader objReader = new StreamReader(objRequest.GetResponse().GetResponseStream()))
            {
                originalBox.Text = objReader.ReadToEnd();
            }
            objRequest = HttpWebRequest.Create(l2URL);

            //Read all lines of file
            String[] crString = { "<BR>&nbsp;" };
            String[] aLines = originalBox.Text.Split(crString, StringSplitOptions.RemoveEmptyEntries);
            String noHtml = String.Empty;

            for (int x = 0; x < aLines.Length; x++)
            {
                if (aLines[x].Contains(ipaddressBox.Text))
                {
                    noHtml += (RemoveHTML(aLines[x]) + "\r\n");
                }
            }

            //Print results to textbox
            resultsBox.Text = String.Join(Environment.NewLine, noHtml);

        }
    }
    public static string RemoveHTML(string text)
    {
        text = text.Replace("&nbsp;", " ").Replace("<br>", "\n");
        var oRegEx = new System.Text.RegularExpressions.Regex("<[^>]+>");
        return oRegEx.Replace(text, string.Empty);

    } 

I want to go to multiple pages using ASP.NET 4.0, copy all HTML and then finally paste it in a text box. From there I would like to run my parsing function, what is the best way to handle this?

 protected void goButton_Click(object sender, EventArgs e)
    {
        if (datacenterCombo.Text == "BL2")
        {
            fwURL = "http://website1.com/index.html";
            l2URL = "http://website2.com/index.html";
            lbURL = "http://website3.com/index.html";
            l3URL = "http://website4.com/index.html";
            coreURL = "http://website5.com/index.html";

            WebRequest objRequest = HttpWebRequest.Create(fwURL);
            WebRequest layer2 = HttpWebRequest.Create(l2URL);

            objRequest.Credentials = CredentialCache.DefaultCredentials;
            using (StreamReader layer2 = new StreamReader(layer2.GetResponse().GetResponseStream()))


            using (StreamReader objReader = new StreamReader(objRequest.GetResponse().GetResponseStream()))
            {
                originalBox.Text = objReader.ReadToEnd();
            }
            objRequest = HttpWebRequest.Create(l2URL);

            //Read all lines of file
            String[] crString = { "<BR> " };
            String[] aLines = originalBox.Text.Split(crString, StringSplitOptions.RemoveEmptyEntries);
            String noHtml = String.Empty;

            for (int x = 0; x < aLines.Length; x++)
            {
                if (aLines[x].Contains(ipaddressBox.Text))
                {
                    noHtml += (RemoveHTML(aLines[x]) + "\r\n");
                }
            }

            //Print results to textbox
            resultsBox.Text = String.Join(Environment.NewLine, noHtml);

        }
    }
    public static string RemoveHTML(string text)
    {
        text = text.Replace(" ", " ").Replace("<br>", "\n");
        var oRegEx = new System.Text.RegularExpressions.Regex("<[^>]+>");
        return oRegEx.Replace(text, string.Empty);

    } 

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

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

发布评论

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

评论(1

我为君王 2024-12-15 21:44:05

您可能应该使用 HtmlAgilityPack 而不是手动执行所有这些操作,那么您可以执行如下操作:

HtmlWeb web = new HtmlWeb();
HtmlDocument doc = web.Load("http://google.com");

var targetNodes = doc.DocumentNode
                     .Descendants()
                     .Where(x=> x.ChildNodes.Count == 0 
                            &&  x.InnerText.Contains(someIpAddress));

foreach (var node in targetNodes)
{
    //do something
}

如果 HtmlAgilityPack 不是一个选项,至少简化代码的下载部分并使用 WebClient

using (WebClient wc = new WebClient())
{
    string html = wc.DownloadString("http://google.com");
}

Instead of doing all this manually you should probably use HtmlAgilityPack instead then you could do something like this:

HtmlWeb web = new HtmlWeb();
HtmlDocument doc = web.Load("http://google.com");

var targetNodes = doc.DocumentNode
                     .Descendants()
                     .Where(x=> x.ChildNodes.Count == 0 
                            &&  x.InnerText.Contains(someIpAddress));

foreach (var node in targetNodes)
{
    //do something
}

If HtmlAgilityPack is not an option for you, simplify at least the download portion of your code and use a WebClient:

using (WebClient wc = new WebClient())
{
    string html = wc.DownloadString("http://google.com");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文