Hostname Resolution without editing Host File

Have you ever thought to map hostname without changing host file? If your Sitecore or any other website is working with Load Balanced environment with multiple servers, you surely have thought to point your request to a particular server (IP Address). When you need to check your functionality working/not working on any of these servers, you are surely going to make host entry for individual servers one by one and test the site.

Uffff... it's so boring and lengthy task and chances of human mistakes. Let's see how we can automate this.

Traditional way for Host Name Resolution Using a Hosts File

Find Hosts file in the %SystemRoot%\system32\drivers\etc directory. Read more about Host Name Resolution. Following is an example of the contents of the Hosts file:
#
# Table of IP addresses and host names
#

127.0.0.1 localhost
192.168.220.111 www.patelyogesh.in # Server-1
#192.168.220.112 www.patelyogesh.in # Server-2

How we can bypass hosts file for host name resolution

Traditional way to get response using WebRequest is requesting to http://www.patelyogesh.in but this will get content from any of the servers which Load Balancer choose for our request.

We can get content from particular server by requesting to: http://<IP Address>/<Page> and passing host/domain name as request.Host as below:
      
var request = (HttpWebRequest)WebRequest.Create("http://<IP Address>/<Page>");
request.Host = HostName;
Let us see how whole source code / GetResult function will be to achieve this:
      string strIPAddress = "192.168.220.111";
      string strHostName = "www.patelyogesh.in";

      string result = GetResult(strIPAddress, strHostName);

      public string GetResult(string strIPAddress, string strHostName)
      {
            var request = (HttpWebRequest)WebRequest.Create("http://" + strIPAddress);
            request.Host = strHostName;

            request.Credentials = CredentialCache.DefaultCredentials;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            // Get the stream containing content returned by the server.
            Stream dataStream = response.GetResponseStream();

            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);

            // Read the content. 
            string responseFromServer = reader.ReadToEnd();

            // Cleanup the streams and the response.
            reader.Close();
            dataStream.Close();
            response.Close();
            return responseFromServer;
      }
Again, www.patelyogesh.in is mapping to 192.168.220.111. Now suppose we want to request to http://www.patelyogesh.in/AboutUs.html page, we can pass parameters as:

var request = (HttpWebRequest)WebRequest.Create("http://192.168.220.111/AboutUs.html");
request.Host = "www.patelyogesh.in";
Thus we can access all the pages using C# .NET 4.5 bypassing host file using above source code.

Hope you like this idea! Cheers!!

2 comments:

  1. Thanks Yogesh.. This is really v helpful info to bypass our load balancer to read content from specific server. I was lookin 4 it in othr forums nd got it here.

    ReplyDelete
  2. Glad to know it worked for you!

    Actually even I searched a lot for this, but thanks to .NET 4.5. In other frameworks, we have to pass host name as request header-Host.

    ReplyDelete