Wednesday, March 02, 2005

Slowwwwww website

Firstly I would like to say congrats to the DotNet Rocks team on reaching their 100th show. Yeah I know I was a couple of weeks ago, but I have only just got arround to listening to it. Very funny clips from various .net people such as Don Box, Dino Esposito and others.

Have you ever had a web site up and running only to have it be slow on the first request??? Well as you probably know, first request can be slower due to the .net compiler doing its' job. I run a site which seems to have a slow response after a while from the first visit. I don't know the complexities of the processes going on, but I have tried cacheing. Well anyway I have written this small console app which gets called every now and then from the standard Windows Scheduler. It simply reads a text file storing URLs' and requests them.

This is the code.


Imports System
Imports System.IO
Imports System.Net
Imports System.Text

Module Module1

Sub Main()
Dim objReaderToCount As New StreamReader("URLFile.txt")
Dim objReader As New StreamReader("URLFile.txt")
Dim p As Integer
Dim strCount As String

p = 0

While objReaderToCount.Peek() > -1


strCount = objReaderToCount.ReadLine


p = p + 1


End While

objReaderToCount.Close()



Dim arrURL(p) As String
Dim I As Integer
Dim strReadLine As String

While objReader.Peek() > -1

For I = 1 To p

strReadLine = objReader.ReadLine

Console.WriteLine(strReadLine)

arrURL(I) = strReadLine

Next

End While



objReader.Close()

Dim intI As Integer

For intI = 1 To p


Dim Request(intI) As HttpWebRequest

Try
Request(intI) = WebRequest.Create(arrURL(intI))

Dim Response(intI) As HttpWebResponse

Response(intI) = Request(intI).GetResponse()

Dim resStream(intI) As Stream

resStream(intI) = Response(intI).GetResponseStream()

Console.WriteLine("Web Request..." + arrURL(intI))
Catch ex As Exception
Console.WriteLine("An error occurred:")

End Try
Next intI
End Sub

End Module



For this to work, you need a file called URLFile.txt in the same directory to read from.

Sorry for the lack of comments, bad habit I know. Well basically it reads each line from URLFile.txt does a web request using WebRequest.Create() and if it gets a response, writes it out to the console. The Try...Catch section handles any exceptions such as bad URL's.

You can compile this using the vbc like this:-

vbc /t:library /r:System.dll WebRequestAuto.exe

When I finally get my web site up and running, I will probs repost this code with the resulting .exe (with comments perhaps)