Click here to Skip to main content
15,881,638 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 !

C#
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;
    }
}


What I have tried:

the code I put in already
Posted
Updated 26-May-18 9:11am

1 solution

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".
 
Share this answer
 
Comments
_Q12_ 26-May-18 15:23pm    
you are right - i corrected the stream with "sr.Close();" in the end.
_Q12_ 26-May-18 15:25pm    
I dont really know how to make robust web access methods. This is my best I could do, and i am using this method for a couple of years - i am happy with it because is working, but i feel it too, that is not that efficient. That's why I asked about it here today. Thank you.
[no name] 27-May-18 12:54pm    
You seemed to imply you were doing "copy and pastes" of this particular method.

That's where I see your main problem; if that's what's you're doing; duplicating code. If not, ok.

(Look at the "Using ..." statement / pattern in c# reference re: disposing of resources / files).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900