Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

JavaWorld Daily Brew

HTTP Status Code 500 Returned - HttpURLConnection to Connect to Web Server Cluster Nodes



Presently, I am attempting to use the HttpURLConnection to make web server calls across multiple nodes participating in a web server cluster. I have a web application 'MyWebAppl' that is running/deployed on 'WebServerA' (Node #1) and the same web application 'MyWebAppl' that is running/deployed on 'WebServerB' (Node #2). From 'WebServerA' I am attempting to open/establish a HttpURLConnection with 'WebServerA' and 'WebServerB' which results in IOException with an HTTP Status Code of 500. I would like to know whether anyone has attempted something similar wherein the client and the server co-exist within the same web application and the the client is trying to connect to itself. Are there any restrictions / limitations to this? I have tried to investigate and search if similar issues have been experienced or reported or if there are any limitations within HttpURLConnection, however I haven't found anything specific that suggests whether or not this is possible. I would appreciate any inputs or proposed approaches in this regard. I am also providing below the code snippet (convenience method) that is being used to make the web server calls.

   /**
    * <p>Invoke the URL</p>
    */
   public boolean invokeUrl(String urlLink, String sessionId)
   throws ClusterException
   {
      URL                 url;
      HttpURLConnection   urlConn = null;
      PrintWriter         outputWriter;
      BufferedReader      bufferedReader;
      InputStream         inputStream = null;
     
      boolean success = false;
     
      try
      {
         // Make the URL invocation for generating the report
         url = new URL(urlLink);
         urlConn = (HttpURLConnection) url.openConnection();
        
         // Set the Cookie for the web server cluster node's session-id.
         urlConn.setRequestProperty("Cookie", sessionId);
        
         urlConn.setDoInput(true);
         urlConn.setDoOutput(true);
         urlConn.setUseCaches(false);
        
         // Obtain the Input Stream for the URL connection. In case the session on the web server node
         // has been terminated, HTTP status code '401' will be returned by the reporting server resulting
         // into an IOException. In this situation the logged in user should be reauthenticated once again
         // on the reporting server and reporting request must be reattempted.
         try
         {
            inputStream = urlConn.getInputStream();
         }
         catch (IOException ioe)
         {
            urlConn.disconnect();
           
            // If HTTP response code '401' is returned then try to resume the expired
            // session on the reporting server by obtaining a new HTTP URL Connection.
            if (urlConn.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED)
            {
               throw new ClusterException("The access to the requested resource was denied; the request may have been made without authorization credentials.");
            }
            // If response code other than '401' is returned then propagate the IOException to the next level.
            else
            {
               throw new ClusterException(ioe.getMessage());
            }
         }
        
         // HTTP Response message status is OK, then proceed with generating/displaying the response
         if (urlConn.getResponseCode() == HttpURLConnection.HTTP_OK)
         {
            success = true;
         }
      }
      catch (MalformedURLException murle)
      {
         throw new ClusterException(murle.getMessage());
      }
      catch (IOException ioe)
      {
         throw new ClusterException(ioe.getMessage());
      }
      finally
      {
         if (urlConn != null)
         {
            urlConn.disconnect();
         }
         return success;
      }
   }