How do we know the server running on another machine is Apache or IIS?

How do we know the server running on another machine is Apache or IIS?

    Requires Free Membership to View

Well if you are really lucky, the following method will return the server name and version in a form something like:

   Server: Microsoft-IIS/5.0

However, this is not as straight-forward as it may seem. First of all, if the site is running a caching Web proxy, you might be connecting to the Web proxy rather than the Web server, which can return invalid results. If the site is using a load balancer, you might also get invalid results. If the site is using a TCP firewall, you could get a response from the firewall machine rather than the Web server, again with invalid results.

   void getServerInfo(String aURL)
   {
      BufferedReader   reader;
      URL              url = new URL(aURL);
      String           host = url.getHost();
      int              port = url.getPort();
      if (port == -1)
         port = 80;
      String           file = url.getFile();
      Socket           socket = new Socket(host, port);
      Writer           writer = new
OutputStreamWriter(socket.getOutputStream());
      Reader           inReader = new
InputStreamReader(socket.getInputStream());
      BufferedReader   bufReader = new BufferedReader(inReader);

      try {
         writer.write("GET " + file + " HTTP/1.0\r\n\n");
         writer.flush();
         String input;
         while ((input = bufReader.readLine()) != null)
         {
            System.out.println(input);
         }
      }
      catch(Exception e)
      {
         System.out.println(e.toString());
      }
      finally
      {
         reader.close();
      }
   }

This was first published in September 2002

Join the conversationComment

Share
Comments

    Results

    Contribute to the conversation

    All fields are required. Comments will appear at the bottom of the article.