Write For Us

Access the Internet Through a .NET Application


The main purpose of this tutorial is to provide you with a guide on how to give your .NET application the ability to access the internet world. Before that, we need to know what we mean by internet applications, what is required to build an internet application, and what Microsoft provides in this regard.


Our tutorial is in two parts. In the first part we will introduce some concepts related to our topic and will be talking about what Microsoft .NET framework provides in this regard. In the second part we will walk step by step together to create and build an internet application using one picked technique, and we will give you a hint on the other techniques that you can use to achieve the same target.

Now, let's start ....

What Is Internet Applications?

Technically we call any program or application that makes use of the internet an internet application. Broadly you can classify internet applications into two main categories which are client applications, and server applications. Client applications request some kind of information or data. Server applications respond to and satisfy requests. A classical example about this scenario is the World Wide Web, where people request information through their browsers, and servers at the other end responding to these requests.

Internet applications are not 100% match of the above classification. There are applications that can be a server to one application and at the same time a client to another. Example about this is a middle tire application acting as a server to a client application and once it received a request it sending a request to another server to be able to satisfy the first client request, and in this case it is acting as a client application.

How Does Internet Application works?

To be able to send a request, the client application firstly identifies the internet resource to request from. Secondly it identifies the communication protocol to use according to the kind of the internet resource. Then the client application constructs the package that will be sent to the server application. The structure of the sent package or message basically depends on the used protocol. This package consists of some kind of headers, the body, and finally the end of the package. The body part of the package is where the request is stored or transmitted.

At server type applications, the server application checks the incoming package to find out whether it is a correct package or not. If it is correct, the server will analyze it, handle the request, then it will issue a response according to the result obtained from serving the request.

So, if you want your application to access the internet, according to the above scenario your application will be responsible of identifying the internet resource, identifying the protocol to use, construct the request package according to the chosen protocol, then sending the request and waiting for a response. After receiving a response your client application will be responsible of checking the correctness of the received response package, then analyzing it to obtain the information you requested. 

As you can see, to write an application like this you will need to handle many tedious details yourself. The good news is that Microsoft .NET handles all of this for you. How?! just read on to get the answer....

Internet Applications in the .NET Framework

The .NET framework provides you with a fantastic managed implementation of internet services that can be integrated easily and quickly into your application. This managed implementation is collected and constructed in the form of namespaces, and classes. Namespaces like "System.Net", and "system.Net.Sockets" can be used to implement both web based and internet based applications.

The .NET framework uses specific classes like "URI", "webRequest", and "webResponse" to handle the aforementioned scenario of  request / response behind the scene.


The "URI" class contains the URI of the internet resource you are seeking. Inside this class the requested internet resource is identified. URI stands for Uniform Resource Identifier. The URI consists of at least three (sometimes four) parts. These parts are:

  • The scheme identifier, which identify the communication protocol.
  • The server identifier, which is the address that uniquely identify the server on the internet. This address can be either a domain name system (DNS) host name or a TCP address.
  • The path identifier, which locates the requested information on the server.
  • Optional query string, by which you can pass information from the client to the server.

As an example, the URI "http://search.microsoft.com/results.aspx?mkt=en-US&setlang=en-US&q=.net" consists of "http" as the scheme identifier (communication protocol), "search.microsoft.com" as the server identifier, "/results.aspx" as the path identifier, and "mkt=en-US&setlang=en-US&q=.net" as the query string. 


The "WebRequest" class is used to make a request to a given resource (URI). By using the "Create" method passing to it the required URI you can get a "WebRequest" for a specific protocol, such as HTTP depending on the scheme part (the protocol part) of the passed URI. By the returned "webRequest" you can access many properties that control both the request to the server and the data stream that is sent when the request is made. You can then use "GetResponse" method to send the request from the client application to the server identified in the server part of the URI. After that, you have to wait till the request be satisfied.


The "WebResponse" class provides a response stream after requesting a URI. It acts as a container for the incoming response. This object is the returned value from the "WebRequest.GetResponse" method. By this class you can access the data returned by the server.

Introducing Pluggable Protocols

By using the above three classes you can access an internet resource, send a request, and receive a response. All this can be done without bothering yourself with the details. What we mean here is that the three aforementioned classes are all what you need to perform internet communication whatever the internet resource you need to connect to. The above three classes are enough to handle any protocol that can be represented as a request / response communication model. This is simply a single interface to retrieve data from multiple Internet protocols.

Pluggable protocols are an implementation of communication services that gives your application the ability to use Internet resources without worrying about the specific details of the protocol that each resource uses. The "WebRequest" and "WebResponse" are the basics of pluggable protocols.

The "WebRequest" class has descendant classes that manage the details of making the actual connection to the Internet according to the protocol used by the resource you want to connect to. For example the "HttpWebRequest" class manages the details of connecting to an internet resource using HTTP protocol. When you call the "WebRequest.Create" method with a URI that begins with http, the returning "WebRequest" instance can be used as is or you can cast it to "HttpWebRequest". Although the returned "WebRequest" is enough and provides all what you need to make a connection, you can typecast it to "HttpWebRequest" to access more protocol specific properties.

For further information

Refer to the online copy of Microsoft Developers Network at http://msdn.microsoft.com or use your own local copy of MSDN.

Tutorial toolbar:  Tell A Friend  |  Add to favorites  |  Feedback  |   

comments powered by Disqus