Class HttpUtils


  • public final class HttpUtils
    extends java.lang.Object
    This class provides utility functions to deal with opening connections, building error messages and closing connections, etc.
    Since:
    1.8.0
    Author:
    Misagh Moayyed
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String buildHttpErrorMessage​(java.net.HttpURLConnection connection)
      Build error message from connection in case of failure
      static void closeConnection​(java.net.HttpURLConnection connection)  
      static java.lang.String encodeQueryParam​(java.lang.String paramName, java.lang.String paramValue)  
      static int getConnectTimeout()  
      static int getReadTimeout()  
      protected static java.net.HttpURLConnection openConnection​(java.net.URL url, java.lang.String requestMethod, java.util.Map<java.lang.String,​java.lang.String> headers)  
      static java.net.HttpURLConnection openDeleteConnection​(java.net.URL url)  
      static java.net.HttpURLConnection openPostConnection​(java.net.URL url)  
      static java.net.HttpURLConnection openPostConnection​(java.net.URL url, java.util.Map<java.lang.String,​java.lang.String> headers)  
      static java.lang.String readBody​(java.net.HttpURLConnection connection)  
      static void setConnectTimeout​(int connectTimeout)  
      static void setReadTimeout​(int readTimeout)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • buildHttpErrorMessage

        public static java.lang.String buildHttpErrorMessage​(java.net.HttpURLConnection connection)
                                                      throws java.io.IOException
        Build error message from connection in case of failure
        Parameters:
        connection - HttpURLConnection
        Returns:
        String by combining response code, message and error stream
        Throws:
        java.io.IOException - an IO exception
      • openPostConnection

        public static java.net.HttpURLConnection openPostConnection​(java.net.URL url)
                                                             throws java.io.IOException
        Throws:
        java.io.IOException
      • openPostConnection

        public static java.net.HttpURLConnection openPostConnection​(java.net.URL url,
                                                                    java.util.Map<java.lang.String,​java.lang.String> headers)
                                                             throws java.io.IOException
        Throws:
        java.io.IOException
      • openDeleteConnection

        public static java.net.HttpURLConnection openDeleteConnection​(java.net.URL url)
                                                               throws java.io.IOException
        Throws:
        java.io.IOException
      • openConnection

        protected static java.net.HttpURLConnection openConnection​(java.net.URL url,
                                                                   java.lang.String requestMethod,
                                                                   java.util.Map<java.lang.String,​java.lang.String> headers)
                                                            throws java.io.IOException
        Throws:
        java.io.IOException
      • readBody

        public static java.lang.String readBody​(java.net.HttpURLConnection connection)
                                         throws java.io.IOException
        Throws:
        java.io.IOException
      • encodeQueryParam

        public static java.lang.String encodeQueryParam​(java.lang.String paramName,
                                                        java.lang.String paramValue)
      • closeConnection

        public static void closeConnection​(java.net.HttpURLConnection connection)
      • getConnectTimeout

        public static int getConnectTimeout()
      • setConnectTimeout

        public static void setConnectTimeout​(int connectTimeout)
      • getReadTimeout

        public static int getReadTimeout()
      • setReadTimeout

        public static void setReadTimeout​(int readTimeout)