Class Net

java.lang.Object
com.pnfsoftware.jeb.util.net.Net
All Implemented Interfaces:
INet

public class Net extends Object implements INet
A network object used to download from and upload to http/https URLs, with socks and http proxy support.
  • Constructor Details

    • Net

      public Net()
      Create a standard Net object, using global proxy settings set using setGlobalProxyInformation(NetProxyInfo). The user-agent is set to "PNF Software UP"; the timeouts are set to 30 seconds for connect, 120 seconds for read, 60 seconds for write; redirects are followed.
    • Net

      public Net(Net net0)
      Copy constructor.
      Parameters:
      net0 - an existing net object
  • Method Details

    • setGlobalProxyInformation

      public static void setGlobalProxyInformation(NetProxyInfo proxyinfo)
      Set global proxy information used by newly built clients.
      Parameters:
      proxyinfo - proxy information, or null for a direct connection
    • getGlobalProxyInformation

      public static NetProxyInfo getGlobalProxyInformation()
      Returns:
      optional global proxy information
    • duplicate

      public INet duplicate()
      Description copied from interface: INet
      Duplicate this object to perform safe parameter update.
      Specified by:
      duplicate in interface INet
      Returns:
      a new net object with the same properties as this object (timeouts, user-agent, etc.)
    • getProxyInformation

      public NetProxyInfo getProxyInformation()
      Get proxy information used by this instance.
      Returns:
      optional proxy information
    • setUserAgent

      public void setUserAgent(String userAgent)
      Specified by:
      setUserAgent in interface INet
      Parameters:
      userAgent - a user-agent
    • getUserAgent

      public String getUserAgent()
      Specified by:
      getUserAgent in interface INet
      Returns:
      the user-agent
    • setConnectTimeout

      public void setConnectTimeout(int timeout)
      Specified by:
      setConnectTimeout in interface INet
      Parameters:
      timeout - in milliseconds
    • getConnectTimeout

      public int getConnectTimeout()
      Specified by:
      getConnectTimeout in interface INet
      Returns:
      timeout in milliseconds
    • setReadTimeout

      public void setReadTimeout(int timeout)
      Specified by:
      setReadTimeout in interface INet
      Parameters:
      timeout - timeout in milliseconds
    • getReadTimeout

      public int getReadTimeout()
      Specified by:
      getReadTimeout in interface INet
      Returns:
      timeout in milliseconds
    • setWriteTimeout

      public void setWriteTimeout(int timeout)
      Specified by:
      setWriteTimeout in interface INet
      Parameters:
      timeout - timeout in milliseconds
    • getWriteTimeout

      public int getWriteTimeout()
      Specified by:
      getWriteTimeout in interface INet
      Returns:
      timeout in milliseconds
    • getSecureSocketInfo

      public SecureSocketInfo getSecureSocketInfo()
      Specified by:
      getSecureSocketInfo in interface INet
      Returns:
      optional secure socket customization information
    • setSecureSocketInfo

      public void setSecureSocketInfo(SecureSocketInfo info)
      Specified by:
      setSecureSocketInfo in interface INet
      Parameters:
      info - optional secure socket customization information
    • buildClient

      public okhttp3.OkHttpClient buildClient(String url)
      Parameters:
      url - optional but recommended if a proxy is in place
      Returns:
      an HTTP client configured with this instance's settings
    • buildClient

      public okhttp3.OkHttpClient buildClient(String url, IProgressCallback progressCallback)
      Parameters:
      url - optional but recommended if a proxy is in place
      progressCallback - optional
      Returns:
      an HTTP client configured with this instance's settings
    • buildClient

      public okhttp3.OkHttpClient buildClient(String url, IProgressCallback progressCallback, int[] timeouts)
      Parameters:
      url - optional but recommended if a proxy is in place
      progressCallback - optional
      timeouts - optional (if null, the object's timeouts are used); else, a 3-element array of timeouts in ms: connect, read, write; when a timeout entry is specified, the value 0 means infinite (do not time-out), and a negative value means use this object's timeout
      Returns:
      an HTTP client configured with this instance's settings
    • post

      public String post(String url, Map<String,String> urlParams, Map<String,String> bodyParams, Map<String,String> serverHeaders) throws IOException
      Description copied from interface: INet
      Perform an http POST request to send a simple form.
      Specified by:
      post in interface INet
      Parameters:
      url - url with optional parameters
      urlParams - optional additional url parameters
      bodyParams - request content parameters
      serverHeaders - optional map whose keys indicate response headers to retrieve
      Returns:
      the server response
      Throws:
      IOException - on error
    • post

      public String post(String url, Map<String,String> urlParams, Map<String,String> bodyParams) throws IOException
      Description copied from interface: INet
      Perform an http POST request to send a simple form.
      Specified by:
      post in interface INet
      Parameters:
      url - url with optional parameters
      urlParams - optional additional url parameters
      bodyParams - request content parameters
      Returns:
      the server response
      Throws:
      IOException - on error
    • postMultipart

      public String postMultipart(String url, Map<String,String> formParams, Map<String,FormFileEntry> formFiles, Map<String,String> serverHeaders) throws IOException
      Description copied from interface: INet
      Perform an http POST request to send a form using the multipart/form-data encoding.
      Specified by:
      postMultipart in interface INet
      Parameters:
      url - url with optional parameters
      formParams - optional request content parameters
      formFiles - optional request content files
      serverHeaders - optional map whose keys indicate response headers to retrieve
      Returns:
      the server response
      Throws:
      IOException - on error
    • postMultipart

      public String postMultipart(String url, Map<String,String> formParams, Map<String,FormFileEntry> formFiles) throws IOException
      Description copied from interface: INet
      Perform an http POST request to send a multipart form.
      Specified by:
      postMultipart in interface INet
      Parameters:
      url - url with optional parameters
      formParams - optional request content parameters
      formFiles - optional request content files
      Returns:
      the server response
      Throws:
      IOException - on error
    • query

      public String query(String url, Map<String,String> urlParams, Map<String,String> serverHeaders) throws IOException
      Description copied from interface: INet
      Perform an http GET request.
      Specified by:
      query in interface INet
      Parameters:
      url - url with optional parameters
      urlParams - optional additional url parameters
      serverHeaders - optional map whose keys indicate response headers to retrieve
      Returns:
      the server response
      Throws:
      IOException - on error
    • query

      public String query(String url, Map<String,String> parameters) throws IOException
      Description copied from interface: INet
      Perform an http GET request.
      Specified by:
      query in interface INet
      Parameters:
      url - url with optional parameters
      parameters - optional additional url parameters
      Returns:
      the server response
      Throws:
      IOException - on error
    • query

      public String query(String url) throws IOException
      Description copied from interface: INet
      Perform an http GET request.
      Specified by:
      query in interface INet
      Parameters:
      url - url with optional parameters
      Returns:
      the server response
      Throws:
      IOException - on error
    • queryBinary

      public byte[] queryBinary(String url, Map<String,String> urlParams, Map<String,String> serverHeaders, IProgressCallback progressCallback) throws IOException
      Description copied from interface: INet
      Perform an http GET request.
      Specified by:
      queryBinary in interface INet
      Parameters:
      url - url with optional parameters
      urlParams - optional additional url parameters
      serverHeaders - optional map whose keys indicate response headers to retrieve
      progressCallback - optional listener
      Returns:
      the server response
      Throws:
      IOException - on error
    • queryBinary

      public byte[] queryBinary(String url, Map<String,String> urlParams, Map<String,String> serverHeaders) throws IOException
      Description copied from interface: INet
      Perform an http GET request.
      Specified by:
      queryBinary in interface INet
      Parameters:
      url - url with optional parameters
      urlParams - optional additional url parameters
      serverHeaders - optional map whose keys indicate response headers to retrieve
      Returns:
      the server response
      Throws:
      IOException - on error
    • queryBinary

      public byte[] queryBinary(String url, Map<String,String> parameters) throws IOException
      Description copied from interface: INet
      Perform an http GET request.
      Specified by:
      queryBinary in interface INet
      Parameters:
      url - url with optional parameters
      parameters - optional additional url parameters
      Returns:
      the server response
      Throws:
      IOException - on error
    • queryBinary

      public byte[] queryBinary(String url) throws IOException
      Description copied from interface: INet
      Perform an http GET request.
      Specified by:
      queryBinary in interface INet
      Parameters:
      url - url with optional parameters
      Returns:
      the server response
      Throws:
      IOException - on error
    • downloadBinary

      public long downloadBinary(File dstfile, String url, Map<String,String> urlParams, Map<String,String> serverHeaders, IProgressCallback progressCallback) throws IOException
      Description copied from interface: INet
      Download a binary and dump it to disk. Similar to INet.queryBinary(String, Map, Map, IProgressCallback), but the downloaded data is not stored and returned, therefore making this method more suitable to download Large files.
      Specified by:
      downloadBinary in interface INet
      Parameters:
      dstfile - destination file
      url - url with optional parameters
      urlParams - optional additional url parameters
      serverHeaders - optional map whose keys indicate response headers to retrieve
      progressCallback - optional listener
      Returns:
      the number of bytes written
      Throws:
      IOException - on error
    • convertHttpDateToEpoch

      public static long convertHttpDateToEpoch(String dateString)
      An HTTP Date.
      Parameters:
      dateString - HTTP date string
      Returns:
      epoch in ms; 0 on error (that is, Jan 1 1970)
    • isConnectedToNetwork

      public static boolean isConnectedToNetwork()
      Determine if the system offers at least one active non-loopback network interface. Careful! This cannot be used to reliably find out if the system has an Internet connection.
      Returns:
      true or false
    • isConnectedToInternet

      public static boolean isConnectedToInternet()
      Determine if the system appears to be connected to the Internet.
      Returns:
      true or false