Check which server is running behind a website – C# Tutorial
We know now a days more websites and blogs are all around the Internet. Most of them hosted their blogs in Linux and windows Servers and many more. Today we gonna see how to check the Server of the Website. Yes, through this application you can see which server is running behind a website.
You can Download this Application to check the server of the Website
Download Here
or you want to Create this Application go ahead and see the Tutorial to create.
Before we start, you should be aware of how to create a project in Visual Studio 2005 and also the basics for creating a windows application and forms.
This is how your application will look like after you have learned this tutorial:
Below are the steps that would help you accomplish the task:
Step 1) Create a new Visual Studio project. Make sure that you have selected Windows Application as the Project Type. Well, it works with Console Application as well.
Step 2) Design the UI as given below:
Step 3) Add the namespace required to get the HttpWebRequest class:
using System.Net;Write the below function:
public static string GetServer(string url) { url = (url.StartsWith("http") ? url : "http://" + url).Trim().ToLower(); WebHeaderCollection collection = new WebHeaderCollection(); try { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); request.Method = "HEAD"; collection = request.GetResponse().Headers; } catch (Exception e) { //Console.WriteLine(e.Message); } Return (collection["Server"] == null ? "No Response!" : collection["Server"]); }What the above function does?
Answer: The above function will take the website name as the parameter and will send a request using the HEAD HTTP method. Then the server sends back headers collection as response. From the headers we take just the “Server” key as we would be requiring only this. Now, return the “Server” key value. If there is no response from server side or the server has disabled the sending of “Server” key value, then we will return “No Response!”Steps 4) Now, let’s call this function from our button click:
private void btnSearch_Click(object sender, EventArgs e) { string serverReply = GetServer(txtWebsite.Text); txtReply.Text = serverReply; }Now, let’s run the form and see whether it gives us an output or not.
Yey! We finally got our output.
Now, you must have noticed that the application hangs for some time while it sends the request and gets back the response. So, I would suggest you to use BackgroundWorker component so that you could send Asynchronous requests and get response. This won’t make your application sluggish and will improve the performance of your application.
Now, let’s do the same thing using BackgroundWorker component.
Step 5) Drag a BackgroundWorker component from the Toolbox to your form:
Step 6) Add the below namespace:
using System.Threading;Step 7) Add the following functions:
private void ToggleControls(bool state) { txtWebsite.Enabled = state; btnSearch.Enabled = state; } private void DisableControls() { ToggleControls(false); } private void EnableControls() { ToggleControls(true); }Step 8 ) Add the following code to the Click event of search button:
private void btnSearch_Click(object sender, EventArgs e) { ThreadStart ts1 = new ThreadStart(DisableControls); ts1.Invoke(); txtReply.Text = "Searching..."; backgroundWorker1.RunWorkerAsync(txtWebsite.Text); }Step 9) In the DoWork event of backgroundWorker1, write the following code:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { string serverReply = GetServer((string)e.Argument); e.Result = serverReply; }Step 10) In the RunWorkerCompleted event of backgroundWorker1, write the following code:
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { string reply = (string)e.Result; txtReply.Text = reply; ThreadStart ts2 = new ThreadStart(EnableControls); ts2.Invoke(); }Now, run the final application:
Output:
In this tutorial, I have implemented Threading concept so that when the request is being sent, the TextBox and the Button controls should be locked or disabled and when the response is received, the same should be enabled again.
Finally, you have a full-fledged application to detect the server running in the background of a website. The application can sometimes return “No Response!” if searching for some site where sending of “Server” header key is disabled.
I hope you found it interesting and useful. Happy Coding.
Download the Visual Studio Project for the same: Here
If you like this post, Share it to your friends. Dont forget to Subscribe our Feeds, Follow us on Twitter and Facebook.
If you like this post, Share it to your friends. Dont forget to Subscribe our Feeds, Follow us on Twitter, Facebook and Pinterest.
Nice Share Kamal…. will try it on my Blog.. would like to hear more from you.. 🙂
Sure.. just subscribe our feeds you can get our all updates on ur mail box 🙂
Thanks Rohit, Well, very soon I’ll be putting more c# demos/tutorials and apps. Anyways, thanks for liking it. 🙂
I’ll try to put this to good use immediately.
Thanks for your comment