tags:

views:

40

answers:

1

hi, I need to do program in c# which opens an internet page and after the page finish to load ("done") , I need to take a timestamp to see how much time it took.
How can I do this?

+3  A: 

Hi,

You need something like this:

        WebRequest myWebRequest = WebRequest.Create(strURL);  
        WebResponse myWebResponse = myWebRequest.GetResponse();  
        Stream ReceiveStream = myWebResponse.GetResponseStream(); 
        Encoding encode = System.Text.Encoding.GetEncoding("utf-8"); 
        StreamReader readStream = new StreamReader( ReceiveStream, encode ); 
        string strResponse=readStream.ReadToEnd(); 
        StreamWriter oSw=new StreamWriter(strFilePath); 
        oSw.WriteLine(strResponse); 
        oSw.Close(); 
        readStream.Close(); 
        myWebResponse.Close(); 

Measure the time before and after the code above to see how much it took.

Thanks, Flores

Flores
KhanZeeshan
thanks, in the strURL i need to enter my url? it gives me some troubles when i write "www.google.com" what's the problem? in the strFilePath i need to enter the name of the file in which i write the data?
yishai
BTW, when i run the program it doesn't luanch IE, how do i know that the website is working? BTW, do i need to write this code in asp.net or regular form application is enough?
yishai
Yes, strUrl should be the url that you try to measure. Try to enter a http:// in front of the site name. strFilePath is the file where the page will be downloaded on your computer. The program will not lauch IE. To see if the page was downloaded, you need to see that file on the disk (check the content). You can place this code in any .NET application.
Flores
just to comlete the answer, i'm using the System.IO.StreamWriter in order to write to a file, but now it doesn't write to my file (i've used this method in the past). do you know what the problem is?
yishai
never mind, i did it.
yishai
glad you did it
Flores
the time i'm messuring is only until the response time and not the time the page is fully loaded, how can i wait until the page is fully loaded?
yishai
ok, i need to run this code once in 30 minutes, i've tried to put timer between them, how i make the code wait until the timer runs out?
yishai
if you need this to run every 30 minutes, you would better take a look at how to build a windows service. it would be the best way to run this repeating task. and to answer the previous question, string strResponse=readStream.ReadToEnd(); reads the page to the end. So the page is fully loaded when this ends.
Flores
thanks alot for all the answer! found it: http://stackoverflow.com/questions/3882818/setting-up-an-elapsed-timer-event
yishai