When compared to an http script written in standard ASP, this
script really illustrates how important a role objects play
in the new .NET framework. It seems like every object returns
an object, and while it may take some getting used to, it really
does seem to make more sense.
That being said... it'll take a while to get used to! ;)
I recently (Jan. 16, 2002) got an interesting note about this
sample from one of our readers and I thought that I
should share it with everyone:
Hi John,
I'm writting in regards to your article at http://www.asp101.com/samples/http.asp ~ June 2001?
I forget how I fell upon it just now and I'm sure you've long since forgotten the code, but it
did remind me of how, looking forward as I am to .net and, likely, C#, I'll miss VB's with
statement...
One glance at (hopefully hotmail will preserve the formatting)...
Function GetWebPageAsStringShort(strURI As String) As String
Dim objURI As URI = New URI(strURI)
Dim objWebRequest As WebRequest = WebRequest.Create(objURI)
Dim objWebResponse As WebResponse = objWebRequest.GetResponse()
Dim objStream As Stream = objWebResponse.GetResponseStream()
Dim objStreamReader As StreamReader = New StreamReader(objStream)
Dim strHTML As String = objStreamReader.ReadToEnd
GetWebPageAsStringShort = strHTML
End Function
...and I couldn't resist whipping up...
Function GetWebPageAsStringShort2(strURI As String) As String
With WebRequest.Create(New URI(strURI)).GetResponse()
With New StreamReader(.GetResponseStream())
GetWebPageAsStringShort2 = .ReadToEnd()
End With
End With
End Function
Admittedly, I'm partial to my version, but I particularly like how the use
of the with statement results in a fn that introduces a reader to only two
new tokens, the names of the fn and its arg. To be certain, I'm not sure of
the details of .net memory management, but, I believe this version is at
least as prudent an implementation since the compiler can early-bind with
references, perhaps more so as I also create and leave what I can on the
stack.
The manner in which indentation in conjunction with progressively shorter
lines of code focuses the eye gradually towards the end result is an
especially neat side effect. (Yes, I studied visual arts prior to cs ;~)
Of course, it is but one of so many bits of code we'll write in a lifetime.
And, I've spent more time writting this email than the fn!
What do you think?
Take Care,
JT from Boston, MA.