HTTP Tunneling Revealed: Part 2/3 - Implementation in .NET Using System.Net.WebClient
(Page 4 of 5 )
Keeping backward compatibility in mind, the .NET framework has provided an enhanced set of libraries to deal with programming problems and develop solutions. Specially, by using .NET I/O and Networking libraries, what were considered much difficult tasks before can now be made simpler and intuitive.
I'm demonstrating a simple example of this below, after a brief intro of the System.Net.WebClient class. As described in GNU (see notes), the CLI (Common Language Interface) supports URI's with the "http:", "https:", and "file:" schemes.
The WebClient class provides the following methods for uploading data to a resource:
- System.Net.WebClient.OpenWrite(System.String) sends a stream to the server hosting a resource.
- System.Net.WebClient.UploadData(System.String,System.Byte[]) sends a byte array to the server hosting a resource and returns a byte array containing the response from the server, if any.
- System.Net.WebClient.UploadFile(System.String,System.String) sends a local file to the server hosting a resource and returns a byte array containing the response from the server, if any.
- System.Net.WebClient.UploadValues(System.String,System.Collections.Specialized.NameValueCollection) sends a NameValueCollection collection to the server hosting a resource and returns a byte array containing the response from the server, if any.
The WebClient class also provides the following methods for downloading data from a resource:
- System.Net.WebClient.DownloadData(System.String) downloads data from a resource and returns a byte array.
- System.Net.WebClient.DownloadFile(System.String,System.String) downloads data from a resource to a local file.
- System.Net.WebClient.OpenRead(System.String) returns the data from the resource as a Stream.
In the example below, the most commonly used function of the webclient class, DownloadData, is used to demonstrate how easy it is to do what once was very difficult to perform using third part or other components:
<%
dim refinedstuff, pos1, pos2
dim webC As New System.Net.WebClient()
dim stuff as Byte()
'//this function returns a byte array
stuff= webC.DownloadData("http://www.census.gov/cgi-bin/ipc/popclockw")
refinedstuff = Encoding.ASCII.GetString(stuff)
pos1 = instr(refinedstuff, "<H1>") : pos2 = instr(refinedstuff, "</H1>")
Response.write ("As per statistics from www.census.gov/cgi-bin/ipc/popclockw, <br>" )
Response.write ("the world population is estimated to be " & mid (refinedstuff,pos1+4, pos2-pos1-4))s
%>
File Uploading in ASP.NET from a Remote Server ASP.NET has made this task even easier than ever before. What I found most interesting is the I/O in Graphic Context System libraries, i.e. similar function handling writing streams for both HDD and web browser:
<%
' instantiation of WebClient
dim webObj AS New System.Net.WebClient()
'// Declaring and Setting URL Variable
dim strURL
strURL ="http://msdn.microsoft.com/library/shared/toolbar/graphics/banners/MSDN_banner.gif"
'// Instance of Drawing Bitmap object based on strURL
dim graphicContext = New System.Drawing.Bitmap(webObj.OpenRead(strURL ))
'//Setting Response types for web client
response.contenttype="image/gif"
'// Saving it on HDD
graphicContext.save("C:\\Documents\\logo2.gif", graphicContext.rawformat)
'// Writing it on web client
graphicContext.save(response.outputstream, graphicContext.rawformat)
' Collecing the garbage & dereferencing
graphicContext.dispose()
webObj = nothing
%>
Web Services In .NET I have already explained how InterPressfact's getjoke web service works within a SOAP envelope using legacy ServerXMLHTTP, now lets move on with the development in .NET Framework.
In the Microsoft SDK for .NET, there is a tool called "Web Services Description Language tool" (WSDL.exe). This command line executable is used to create proxy classes from WSDL. For instance, if the WSDL is located at http://www.interpressfact.net/webservices/getJoke.asmx?wsdl, then we would use the following command to create a proxy class called getJoke.cs after the name of class defined in web Service:
wsdl http://www.interpressfact.net/webservices/getJoke.asmx?wsdl 
Next: Conclusion >>
More ASP Articles
More By Adnan Masood