Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Stardeveloper RSS Feed
Newsletter
Enter your email address below to be informed every time a new article is posted at Stardeveloper.com:

You can follow Faisal Khan on Twitter
Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (41)
  ADO (16)
  ADO.NET (10)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)
Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Submit Article

Hosted by Securewebs.com
 
Home : .NET : ASP.NET : How to determine what server is given web site running on using ASP.NET?
 

How to determine what server is given web site running on using ASP.NET?

by Faisal Khan.Follow Faisal Khan on Twitter

Overview
When surfing web sites, some times when you see a decent application of some sort e.g. forum, news site etc, you might have wanted to know what type of application is this? what is it running on? Well, this article seems to answer that. In this article we'll build a very simple ASP.NET single page application that will allow us to see the server of any given web site. Although this method doesn't tell us what operating system that site is running on, if you are clever enough you can make a good guess by looking at the server name.

How will the application look like?
Before explaining how the ASP.NET application will work, I'll first show few snapshots to give you an idea what this ASP.NET page will look like and what are we really going to accomplish.

When you first run that ASP.NET page you'll see something like this in your browser:

Check Server Form
Check Server Form

This form shows you an input box to enter the URL of the site whose server name you want to see. Let's say you enter 'www.stardeveloper.com'. It looks like this:

Check Server Form : www.stardeveloper.com
Check Server Form : www.stardeveloper.com

When you pres 'Submit', it shows you the results like this:

Check Server Form : www.stardeveloper.com Server
Check Server Form : www.stardeveloper.com Server

You'll get to know what server is that web site running on plus all the headers returned by that server.

How does one know what server is a given web site running on?
When a browser requests a document from the server, along with the path of the document it sends some extra headers e.g. User-Agent. The programs running on the server can retrieve the value of this header to know what type of client has sent the request. Although neither the client ( browser ) is required to send this 'User-Agent' header nor it is supposed to be technically correct i.e. there is nothing stopping a Netscape browser from sending a Microsoft Internet Explorer like header thus fooling the server to think that the client is IE, which in fact is a Netscape browser.

Similarly when the server replies to this request by sending the response back to the client, it sends along with document request some extra information in the form of headers. One of the useful headers is 'Server' header which like 'User-Agent' header tells the client ( browser ) what server this request has come from. As was the case with 'User-Agent' header sent by the client, neither the server is required to send 'Server' header info nor it is supposed to be correct e.g. an Apache web server might send back a header like that of Microsoft IIS thus fooling the client in thinking that it has requested the document from IIS which in fact was Apache.

If that's the case then how useful is this 'Server' header?
Very useful, in more than 90% of the cases it'll give you very good idea of what server is that site running on because most of the times the server is honest in sending it's name to the client.

Plus not only will you be able to get the server name but some extra plug-ins loaded by that server too e.g. Apache server might return a status line telling what modules it is loaded with e.g. mod_php, mod_perl etc.

Now since you've got a very good idea about what we are up to, what this information will look like and how useful it may be, let's move forward to build this one page simple but useful ASP.NET application.

checkServer.aspx
Create a new 'checkServer.aspx' ASP.NET page under a IIS .NET application folder. A .NET application folder is a simple IIS virtual application folder. If you are still uncertain, then have a look at some of the beginner articles on ASP.NET at this site.

Tip: If you have been running ASP.NET pages before, just place this checkServer.aspx page in the folder with those ASP.NET pages if you are unsure where to put it.

Now copy/paste following code in it and then save the file:

<%@ Page language="c#" AutoEventWireup="false" %>
<%@ Import namespace="System.Net" %>
<%@ Import namespace="System.Text" %>

<script runat="server">
  public void DisplayServer(object sender,
    System.Web.UI.WebControls.CommandEventArgs e)
    {
      string url = Request.Form["url"];

      if(url == null || url.Equals("") || url.Equals("http://"))
      {
        label.Text = "<span class=\"error\">URL value not provided" + 
          "</span>";
        label1.Text = "...";
        return;
      }

      try
      {
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
        HttpWebResponse res = (HttpWebResponse)req.GetResponse();

        // Set Server Name
        string server = res.Server;
        
        if(server == null || server.Length == 0)
        {
          label.Text = "<span class=\"error\">Not Found</span>";
        }
        else
        {
          label.Text = server;
        }

        StringBuilder sb = new StringBuilder();
        sb.Append("<ul>");        

        for(int i = 0; i < res.Headers.Count; i++)
        {
          sb.Append("<li>");
          sb.Append(res.Headers.Keys[i]);
          sb.Append(" : ");
          sb.Append(res.Headers[i]);
          sb.Append("</li>");
        }

        sb.Append("</ul>");

        // Set All Headers
        label1.Text = sb.ToString();
        res.Close();
      }
      catch (Exception ex)
      {
        label.Text = "<span class=\"error\">" + ex.Message +
          "</span>";
        label1.Text = "...";
      }
    }
</script>

<html>
<head>
	<title>checkServer</title>
	<style>
	body { font-family:Verdana; }
	.heading { background-color:#CCDDEE; }
	.form { paddin>op:5; }
	.error { color:red; }
	.stdInput { width:500; font-family:Verdana; margin-top:5; }
	.stdButton { font-family:Verdana; margin-top:5; }
	</style>
</head>
<body>

	<table align="center" width="90%" border="1" cellspacing="2"
		cellpadding="2" bordercolor="silver">
	<tr>
		<td class="heading">Check Server</td>
	</tr>
	<tr>
		<td align="center" class="form">
		<form action="checkServer.aspx" method="post" runat="server">
		Enter URL:
			<input type="text" name="url" value="http://"
				class="stdInput"><br>

			<asp:Button ID="button" Text="Submit"
				CssClass="stdButton"
				OnCommand="DisplayServer"
				Runat="server" />
		</form>
		</td>
	</tr>
	</table>
	
	<br><br>
	
	<% if(IsPostBack) { %>
	<table align="center" width="90%" border="1" cellspacing="2"
		cellpadding="2" bordercolor="silver">
	<tr>
		<td class="heading">Server Name</td>
	</tr>
	<tr>
		<td align="center" class="form">
		<asp:Label ID="label" Runat="server" />
		</td>
	</tr>
	</table>
	
	<br><br>

Overview
When surfing web sites, some times when you see a decent application of some sort e.g. forum, news site etc, you might have wanted to know what type of application is this? what is it running on? Well, this article seems to answer that. In this article we'll build a very simple ASP.NET single page application that will allow us to see the server of any given web site. Although this method doesn't tell us what operating system that site is running on, if you are clever enough you can make a good guess by looking at the server name.

How will the application look like?
Before explaining how the ASP.NET application will work, I'll first show few snapshots to give you an idea what this ASP.NET page will look like and what are we really going to accomplish.

When you first run that ASP.NET page you'll see something like this in your browser:

Check Server Form
Check Server Form

This form shows you an input box to enter the URL of the site whose server name you want to see. Let's say you enter 'www.stardeveloper.com'. It looks like this:

Check Server Form : www.stardeveloper.com
Check Server Form : www.stardeveloper.com

When you pres 'Submit', it shows you the results like this:

Check Server Form : www.stardeveloper.com Server
Check Server Form : www.stardeveloper.com Server

You'll get to know what server is that web site running on plus all the headers returned by that server.

How does one know what server is a given web site running on?
When a browser requests a document from the server, along with the path of the document it sends some extra headers e.g. User-Agent. The programs running on the server can retrieve the value of this header to know what type of client has sent the request. Although neither the client ( browser ) is required to send this 'User-Agent' header nor it is supposed to be technically correct i.e. there is nothing stopping a Netscape browser from sending a Microsoft Internet Explorer like header thus fooling the server to think that the client is IE, which in fact is a Netscape browser.

Similarly when the server replies to this request by sending the response back to the client, it sends along with document request some extra information in the form of headers. One of the useful headers is 'Server' header which like 'User-Agent' header tells the client ( browser ) what server this request has come from. As was the case with 'User-Agent' header sent by the client, neither the server is required to send 'Server' header info nor it is supposed to be correct e.g. an Apache web server might send back a header like that of Microsoft IIS thus fooling the client in thinking that it has requested the document from IIS which in fact was Apache.

If that's the case then how useful is this 'Server' header?
Very useful, in more than 90% of the cases it'll give you very good idea of what server is that site running on because most of the times the server is honest in sending it's name to the client.

Plus not only will you be able to get the server name but some extra plug-ins loaded by that server too e.g. Apache server might return a status line telling what modules it is loaded with e.g. mod_php, mod_perl etc.

Now since you've got a very good idea about what we are up to, what this information will look like and how useful it may be, let's move forward to build this one page simple but useful ASP.NET application.

checkServer.aspx
Create a new 'checkServer.aspx' ASP.NET page under a IIS .NET application folder. A .NET application folder is a simple IIS virtual application folder. If you are still uncertain, then have a look at some of the beginner articles on ASP.NET at this site.

Tip: If you have been running ASP.NET pages before, just place this checkServer.aspx page in the folder with those ASP.NET pages if you are unsure where to put it.

Now copy/paste following code in it and then save the file:

<%@ Page language="c#" AutoEventWireup="false" %>
<%@ Import namespace="System.Net" %>
<%@ Import namespace="System.Text" %>

<script runat="server">
  public void DisplayServer(object sender,
    System.Web.UI.WebControls.CommandEventArgs e)
    {
      string url = Request.Form["url"];

      if(url == null || url.Equals("") || url.Equals("http://"))
      {
        label.Text = "<span class=\"error\">URL value not provided" + 
          "</span>";
        label1.Text = "...";
        return;
      }

      try
      {
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
        HttpWebResponse res = (HttpWebResponse)req.GetResponse();

        // Set Server Name
        string server = res.Server;
        
        if(server == null || server.Length == 0)
        {
          label.Text = "<span class=\"error\">Not Found</span>";
        }
        else
        {
          label.Text = server;
        }

        StringBuilder sb = new StringBuilder();
        sb.Append("<ul>");        

        for(int i = 0; i < res.Headers.Count; i++)
        {
          sb.Append("<li>");
          sb.Append(res.Headers.Keys[i]);
          sb.Append(" : ");
          sb.Append(res.Headers[i]);
          sb.Append("</li>");
        }

        sb.Append("</ul>");

        // Set All Headers
        label1.Text = sb.ToString();
        res.Close();
      }
      catch (Exception ex)
      {
        label.Text = "<span class=\"error\">" + ex.Message +
          "</span>";
        label1.Text = "...";
      }
    }
</script>

<html>
<head>
	<title>checkServer</title>
	<style>
	body { font-family:Verdana; }
	.heading { background-color:#CCDDEE; }
	.form { paddin>op:5; }
	.error { color:red; }
	.stdInput { width:500; font-family:Verdana; margin-top:5; }
	.stdButton { font-family:Verdana; margin-top:5; }
	</style>
</head>
<body>

	<table align="center" width="90%" border="1" cellspacing="2"
		cellpadding="2" bordercolor="silver">
	<tr>
		<td class="heading">Check Server</td>
	</tr>
	<tr>
		<td align="center" class="form">
		<form action="checkServer.aspx" method="post" runat="server">
		Enter URL:
			<input type="text" name="url" value="http://"
				class="stdInput"><br>

			<asp:Button ID="button" Text="Submit"
				CssClass="stdButton"
				OnCommand="DisplayServer"
				Runat="server" />
		</form>
		</td>
	</tr>
	</table>
	
	<br><br>
	
	<% if(IsPostBack) { %>
	<table align="center" width="90%" border="1" cellspacing="2"
		cellpadding="2" bordercolor="silver">
	<tr>
		<td class="heading">Server Name</td>
	</tr>
	<tr>
		<td align="center" class="form">
		<asp:Label ID="label" Runat="server" />
		</td>
	</tr>
	</table>
	
	<br><br>

 ( 2 Remaining ) Next

See all comments and questions (post-ad) posted for this tutorial.


Comments/Questions ( Threads: 1, Comments: 1 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. It is not returning any Header or Error but the follwoing message is comming

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed to.

 
© 1999 - 2009 Stardeveloper.com, All Rights Reserved.