// parse the message and extract the name of the exception and the message
        private static IOException GetExceptionFromResponse(HttpURLConnection con)
        {
            IOException e = null;
            string      resp;

            if (con == null)
            {
                return(null);
            }
            try
            {
                resp = con.GetResponseMessage();
            }
            catch (IOException)
            {
                return(null);
            }
            if (resp == null || resp.IsEmpty())
            {
                return(null);
            }
            string exceptionClass = string.Empty;
            string exceptionMsg   = string.Empty;

            string[] rs = resp.Split(";");
            if (rs.Length < 2)
            {
                return(null);
            }
            exceptionClass = rs[0];
            exceptionMsg   = rs[1];
            Log.Info("Error response from HTTP request=" + resp + ";ec=" + exceptionClass + ";em="
                     + exceptionMsg);
            if (exceptionClass == null || exceptionClass.IsEmpty())
            {
                return(null);
            }
            // recreate exception objects
            try
            {
                Type ec = Sharpen.Runtime.GetType(exceptionClass).AsSubclass <Exception>();
                // we are interested in constructor with String arguments
                Constructor <Exception> constructor = ec.GetConstructor(new Type[] { typeof(string
                                                                                            ) });
                // create an instance
                e = (IOException)constructor.NewInstance(exceptionMsg);
            }
            catch (Exception ee)
            {
                Log.Warn("failed to create object of this class", ee);
            }
            if (e == null)
            {
                return(null);
            }
            e.SetStackTrace(new StackTraceElement[0]);
            // local stack is not relevant
            Log.Info("Exception from HTTP response=" + e.GetLocalizedMessage());
            return(e);
        }
        /// <exception cref="System.IO.IOException"/>
        /// <exception cref="Org.Apache.Hadoop.Security.Authentication.Client.AuthenticationException
        ///     "/>
        private static HttpURLConnection Run(URLConnectionFactory factory, Uri url)
        {
            HttpURLConnection conn = null;

            try
            {
                conn = (HttpURLConnection)factory.OpenConnection(url, true);
                if (conn.GetResponseCode() != HttpURLConnection.HttpOk)
                {
                    string msg = conn.GetResponseMessage();
                    throw new IOException("Error when dealing remote token: " + msg);
                }
            }
            catch (IOException ie)
            {
                Log.Info("Error when dealing remote token:", ie);
                IOException e = GetExceptionFromResponse(conn);
                if (e != null)
                {
                    Log.Info("rethrowing exception from HTTP request: " + e.GetLocalizedMessage());
                    throw e;
                }
                throw;
            }
            return(conn);
        }
        /// <summary>Renew a Delegation Token.</summary>
        /// <param name="nnAddr">the NameNode's address</param>
        /// <param name="tok">the token to renew</param>
        /// <returns>the Date that the token will expire next.</returns>
        /// <exception cref="System.IO.IOException"/>
        /// <exception cref="Org.Apache.Hadoop.Security.Authentication.Client.AuthenticationException
        ///     "/>
        public static long RenewDelegationToken(URLConnectionFactory factory, URI nnAddr,
                                                Org.Apache.Hadoop.Security.Token.Token <DelegationTokenIdentifier> tok)
        {
            StringBuilder buf = new StringBuilder(nnAddr.ToString()).Append(RenewDelegationTokenServlet
                                                                            .PathSpec).Append("?").Append(RenewDelegationTokenServlet.Token).Append("=").Append
                                    (tok.EncodeToUrlString());
            HttpURLConnection connection = null;
            BufferedReader    @in        = null;

            try
            {
                connection = Run(factory, new Uri(buf.ToString()));
                @in        = new BufferedReader(new InputStreamReader(connection.GetInputStream(), Charsets
                                                                      .Utf8));
                long result = long.Parse(@in.ReadLine());
                return(result);
            }
            catch (IOException ie)
            {
                Log.Info("error in renew over HTTP", ie);
                IOException e = GetExceptionFromResponse(connection);
                if (e != null)
                {
                    Log.Info("rethrowing exception from HTTP request: " + e.GetLocalizedMessage());
                    throw e;
                }
                throw;
            }
            finally
            {
                IOUtils.Cleanup(Log, @in);
                if (connection != null)
                {
                    connection.Disconnect();
                }
            }
        }