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?

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

    Requires Free Membership to View

    When you register, you'll begin receiving targeted emails from my team of award-winning writers. Our goal is to keep you informed on recent service-oriented architecture (SOA) and SOA-related topics such as integration, governance, Web services, Cloud and more.

    Hannah Smalltree, Editorial Director

    By submitting your registration information to SearchSOA.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchSOA.com is governed by our Terms of Use. You may contact us at webmaster@TechTarget.com.

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