Class Http


  • public class Http
    extends Object
    This is a convenience class to allow creation of request objects on one line with some pre-defined values.
    Author:
    Igor Polevoy
    • Field Detail

      • CONNECTION_TIMEOUT

        public static final int CONNECTION_TIMEOUT
        Connection timeout in milliseconds. Set this value to what you like to override default.
        See Also:
        Constant Field Values
      • READ_TIMEOUT

        public static final int READ_TIMEOUT
        Read timeout in milliseconds. Set this value to what you like to override default.
        See Also:
        Constant Field Values
    • Method Detail

      • post

        public static Post post​(String uri,
                                String content)
        Executes a POST request.
        Parameters:
        uri - url of resource.
        content - content to be posted.
        Returns:
        Post object.
      • post

        public static Post post​(String uri)
        Executes a POST request. Often used to post form parameters:
             Http.post("http://example.com/create").param("name1", "val1");
         
        Parameters:
        uri - url of resource.
        Returns:
        Post object.
      • post

        public static Post post​(String uri,
                                byte[] content)
        Executes a POST request.
        Parameters:
        uri - url of resource.
        content - content to be posted.
        Returns:
        Post object.
      • post

        public static Post post​(String url,
                                byte[] content,
                                int connectTimeout,
                                int readTimeout)
        Executes a POST request.
        Parameters:
        url - url of resource.
        content - content to be posted.
        connectTimeout - connection timeout in milliseconds.
        readTimeout - read timeout in milliseconds.
        Returns:
        Post object.
      • post

        public static Post post​(String url,
                                int connectTimeout,
                                int readTimeout)
        Executes a POST request. Often used to post form parameters:
             Http.post("http://example.com/create").param("name1", "val1");
         
        Parameters:
        url - url of resource.
        connectTimeout - connection timeout in milliseconds.
        readTimeout - read timeout in milliseconds.
        Returns:
        Post object.
      • get

        public static Get get​(String url)
        Executes a GET request.
        Parameters:
        url - url of the resource.
        Returns:
        Get object.
      • get

        public static Get get​(String url,
                              int connectTimeout,
                              int readTimeout)
        Executes a GET request
        Parameters:
        url - url of resource.
        connectTimeout - connection timeout in milliseconds.
        readTimeout - read timeout in milliseconds.
        Returns:
        Get object.
      • put

        public static Put put​(String uri,
                              String content)
        Executes a PUT request.
        Parameters:
        uri - url of resource.
        content - content to be put.
        Returns:
        Put object.
      • put

        public static Put put​(String uri,
                              byte[] content)
        Executes a PUT request.
        Parameters:
        uri - uri of resource.
        content - content to be put.
        Returns:
        Put object.
      • put

        public static Put put​(String url,
                              byte[] content,
                              int connectTimeout,
                              int readTimeout)
        Executes a PUT request.
        Parameters:
        url - url of resource.
        content - content to be "put"
        connectTimeout - connection timeout in milliseconds.
        readTimeout - read timeout in milliseconds.
        Returns:
        Put object.
      • multipart

        public static Multipart multipart​(String url)
        Create multipart request
        Parameters:
        url - URL to send to
        Returns:
        new Multipart request
      • multipart

        public static Multipart multipart​(String url,
                                          int connectTimeout,
                                          int readTimeout)
        Create multipart request
        Parameters:
        url - URL to send to
        connectTimeout - connect timeout
        readTimeout - read timeout
        Returns:
        new Multipart request
      • delete

        public static Delete delete​(String uri)
        Executes a DELETE request.
        Parameters:
        uri - uri of resource to delete
        Returns:
        Delete
      • delete

        public static Delete delete​(String url,
                                    int connectTimeout,
                                    int readTimeout)
        Executes a DELETE request.
        Parameters:
        url - url of resource to delete
        connectTimeout - connection timeout in milliseconds.
        readTimeout - read timeout in milliseconds.
        Returns:
        Delete
      • patch

        public static Patch patch​(String uri,
                                  String content)
        Executes a PATCH request.
        Parameters:
        uri - url of resource.
        content - content to be posted.
        Returns:
        Patch object.
      • patch

        public static Patch patch​(String uri,
                                  byte[] content)
        Executes a PATCH request.
        Parameters:
        uri - url of resource.
        content - content to be posted.
        Returns:
        Patch object.
      • patch

        public static Patch patch​(String url,
                                  byte[] content,
                                  int connectTimeout,
                                  int readTimeout)
        Executes a PATCH request.
        Parameters:
        url - url of resource.
        content - content to be posted.
        connectTimeout - connection timeout in milliseconds.
        readTimeout - read timeout in milliseconds.
        Returns:
        Patch object.
      • map2URLEncoded

        public static String map2URLEncoded​(Map params)
        Converts a map to URL- encoded string. This is a convenience method which can be used in combination with post(String, byte[]), put(String, String) and others. It makes it easy to convert parameters to submit a string:
             key=value&key1=value1;
         
        Parameters:
        params - map with keys and values to be posted. This map is used to build a URL-encoded string, such that keys are names of parameters, and values are values of those parameters. This method will also URL-encode keys and content using UTF-8 encoding.

        String representations of both keys and values are used.

        Returns:
        URL-encided string like: key=value&key1=value1;