Class URLConnectionTools


  • public class URLConnectionTools
    extends Object
    A class that takes care about opening URLConnections and sets the proper timeouts
    Since:
    5.0
    Author:
    Andreas Prlic, Anthony Bradley
    • Method Detail

      • openURLConnection

        public static URLConnection openURLConnection​(URL url,
                                                      int timeout)
                                               throws 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:
        IOException - an error in opening the URL
      • openURLConnection

        public static URLConnection openURLConnection​(URL url)
                                               throws 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:
        IOException - an error in opening the URL
      • getInputStream

        public static InputStream getInputStream​(URL url,
                                                 int timeout)
                                          throws 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:
        IOException - due to an error opening the URL
      • getInputStream

        public static InputStream getInputStream​(URL url)
                                          throws 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:
        IOException - due to an error opening the URL
      • getInputStream

        public static InputStream getInputStream​(URL url,
                                                 boolean acceptGzipEncoding,
                                                 int timeout)
                                          throws 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:
        IOException - due to an error opening the URL
      • doPOST

        public static InputStream doPOST​(URL url,
                                         String data)
                                  throws 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:
        IOException - due to an error opening the URL
      • doPOST

        public static InputStream doPOST​(URL url,
                                         String data,
                                         int timeout)
                                  throws 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:
        IOException - due to an error opening the URL