Ask the Expert

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

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: