用其他方法访问网页? [英] Webpage access with other methods ?

查看:94
本文介绍了用其他方法访问网页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个新课WebPage。我从Form1中的按钮事件调用它。

问题是每次我访问该网站上的新页面时,我都会重新调用整个网站标识过程。当我第一次访问网站时,我认为这种BIG方法只需要一次。我的直觉说我应该在同一个网站上为每个其他页面使用除了这个大代码之外的其他内容。我错了,我应该坚持我拥有和工作的东西?或者我是对的,还有其他方法(After)这个吗?

谢谢!



I have this new class "WebPage". I am calling it from a button event in Form1.
The problem is that I Re-Call this entire process of website identification every time I go to a new page on that website. I think this BIG method is needed only one time, when I first access the website. My intuition say that I should use something else than this big code for each other pages on the same website. I am wrong and I should stick with what I have and working already? Or I am right and there are other methods (After) this one ?
Thank you !

public class WebPage
{

    public string GetText(string url)
    {
        //Special webpage Reading (extract info from page)
        HttpWebRequest request;
        HttpWebResponse response = null;
        Stream stream = null;
        request = (HttpWebRequest)WebRequest.Create(url);
        request.UserAgent = "Foo";
        request.Accept = "*/*";
        response = (HttpWebResponse)request.GetResponse();
        stream = response.GetResponseStream();

        StreamReader sr = new StreamReader(stream, System.Text.Encoding.Default);
        string text = sr.ReadToEnd();
        if (stream != null) stream.Close();
        if (response != null) response.Close();
        return text;
    }
}





我尝试了什么:



我已经放入的代码

推荐答案

除了大与性能无关,你有潜在的内存泄漏,因为你没有正确处理你的溪流。



此外,一旦你有强大开发的Web访问方法,你将它们变成通用并将它们放在库中......所以它不会变得更大。
Besides the fact the "big" has nothing to do with performance, you have potential memory leaks because you are not "disposing" your streams properly.

In addition, once you have "robust" web access methods developed, you make them "generic" and put them in a "library" ... so it's doesn't get "bigger".


这篇关于用其他方法访问网页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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