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?
views:
40answers:
1
+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
2010-10-15 07:53:55
KhanZeeshan
2010-10-15 08:00:38
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
2010-10-15 08:34:40
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
2010-10-15 08:38:34
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
2010-10-15 08:42:08
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
2010-10-15 08:57:24
never mind, i did it.
yishai
2010-10-15 09:07:14
glad you did it
Flores
2010-10-15 09:08:10
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
2010-10-15 09:25:14
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
2010-10-15 09:30:47
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
2010-10-15 09:36:33
thanks alot for all the answer! found it: http://stackoverflow.com/questions/3882818/setting-up-an-elapsed-timer-event
yishai
2010-10-15 09:44:00