Class URLConnectionTools


  • public class URLConnectionTools
    extends java.lang.Object
    A class that takes care about opening URLConnections and sets the proper timeouts
    Since:
    5.0
    Author:
    Andreas Prlic, Anthony Bradley
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int DEFAULT_CONNECTION_TIMEOUT
      The default connection timeout in ms - 15 seconds
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.io.InputStream doPOST​(java.net.URL url, java.lang.String data)
      Do a POST to a URL and return the response stream for further processing elsewhere.
      static java.io.InputStream doPOST​(java.net.URL url, java.lang.String data, int timeout)
      Do a POST to a URL and return the response stream for further processing elsewhere.
      static java.io.InputStream getInputStream​(java.net.URL url)
      Connect to a URL and return result as an InputStream.
      static java.io.InputStream getInputStream​(java.net.URL url, boolean acceptGzipEncoding, int timeout)
      Open a URL and return an InputStream to it if acceptGzipEncoding == true, use GZIPEncoding to compress communication.
      static java.io.InputStream getInputStream​(java.net.URL url, int timeout)
      Connect to server and return result as an InputStream.
      static java.net.URLConnection openURLConnection​(java.net.URL url)
      Open HttpURLConnection.
      static java.net.URLConnection openURLConnection​(java.net.URL url, int timeout)
      Open HttpURLConnection.
      • Methods inherited from class java.lang.Object

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

      • DEFAULT_CONNECTION_TIMEOUT

        public static final int DEFAULT_CONNECTION_TIMEOUT
        The default connection timeout in ms - 15 seconds
        See Also:
        Constant Field Values
    • Constructor Detail

      • URLConnectionTools

        public URLConnectionTools()
    • Method Detail

      • openURLConnection

        public static java.net.URLConnection openURLConnection​(java.net.URL url,
                                                               int timeout)
                                                        throws java.io.IOException
        Open HttpURLConnection. Recommended way to open URL connections in Java 1.7 and 1.8. https://eventuallyconsistent.net/2011/08/02/working-with-urlconnection-and-timeouts/
        Parameters:
        url - URL to open
        timeout - timeout in milli seconds
        Throws:
        java.io.IOException - an error in opening the URL
      • openURLConnection

        public static java.net.URLConnection openURLConnection​(java.net.URL url)
                                                        throws java.io.IOException
        Open HttpURLConnection. Recommended way to open HttpURLConnections, since this take care of setting timeouts properly for java 1.4 and 1.5 uses the DEFAULT_CONNECTION_TIMEOUT (= 15 seconds)
        Parameters:
        url - a URL to open a http connection to
        Returns:
        HttpURLConnect the opened connection
        Throws:
        java.io.IOException - an error in opening the URL
      • getInputStream

        public static java.io.InputStream getInputStream​(java.net.URL url,
                                                         int timeout)
                                                  throws java.io.IOException
        Connect to server and return result as an InputStream. always asks for response to be in GZIP encoded

        The caller is responsible to close the returned InputStream not to cause resource leaks.

        Parameters:
        url - the URL to connect to
        timeout - the timeout for the connection
        Returns:
        an InputStream of response
        Throws:
        java.io.IOException - due to an error opening the URL
      • getInputStream

        public static java.io.InputStream getInputStream​(java.net.URL url)
                                                  throws java.io.IOException
        Connect to a URL and return result as an InputStream. always asks for response to be in GZIP encoded

        The caller is responsible to close the returned InputStream not to cause resource leaks.

        Parameters:
        url - the input URL to be read
        Returns:
        an InputStream of response
        Throws:
        java.io.IOException - due to an error opening the URL
      • getInputStream

        public static java.io.InputStream getInputStream​(java.net.URL url,
                                                         boolean acceptGzipEncoding,
                                                         int timeout)
                                                  throws java.io.IOException
        Open a URL and return an InputStream to it if acceptGzipEncoding == true, use GZIPEncoding to compress communication.

        The caller is responsible to close the returned InputStream not to cause resource leaks.

        Parameters:
        url - the input URL to be read
        acceptGzipEncoding - whether to accept Gzip encoding
        timeout -
        Returns:
        an InputStream of response
        Throws:
        java.io.IOException - due to an error opening the URL
      • doPOST

        public static java.io.InputStream doPOST​(java.net.URL url,
                                                 java.lang.String data)
                                          throws java.io.IOException
        Do a POST to a URL and return the response stream for further processing elsewhere.

        The caller is responsible to close the returned InputStream not to cause resource leaks.

        Parameters:
        url - the input URL to be read
        data - the post data
        Returns:
        an InputStream of response
        Throws:
        java.io.IOException - due to an error opening the URL
      • doPOST

        public static java.io.InputStream doPOST​(java.net.URL url,
                                                 java.lang.String data,
                                                 int timeout)
                                          throws java.io.IOException
        Do a POST to a URL and return the response stream for further processing elsewhere.

        The caller is responsible to close the returned InputStream not to cause resource leaks.

        Parameters:
        url - the input URL to be read
        data - the post data
        timeout -
        Returns:
        an InputStream of response
        Throws:
        java.io.IOException - due to an error opening the URL