com.amx.duet.tools.net.http
Class HttpPostWrapper

java.lang.Object
  extended bycom.amx.duet.tools.net.http.HttpPostWrapper

public class HttpPostWrapper
extends java.lang.Object

This class will handle any and all HTTP type post requests. It works similar to how a web-browser issues an HTTP request to a Web server, by properly formatting your request as per HTTP v1.1 RFC 2616

  • This HttppPostWrapper is to be instantiated by the module.
  • The HttpPostWrapper provides a reusable HTTP connection by keeping the connection alive. This is desirable for a number of reason from better resource use to more efficient communications.
  • The HttpPostWrapper establishes an HTTP connection on a separate thread, therefore it is non-blocking. Events are generated by invoking asynchronous callback methods as implemented in the IHttpPostWrapper interface.
  • You must implement the IHttpPostWrapper interface if you want to use this class. You should not create several instances of this class unless you are trying to post HTTP request to several different hosts. If you will always use the same host (i.e. http://192.168.103.201 for example) you should not create several instances of this class.

    By default, this Http wrapper will assume a Content-Type of text/html and a Keep-Alive connection.

    This wrapper can use the POST or GET methods to send information to a Web server. Default is POST.

    When the user enters information there are two ways the information can be sent from the browser to the server: in the URL, or within the body of the HTTP request. The GET method, appends name/value pairs to the URL. Unfortunately, the length of a URL is limited, so this method only works if there are only a few parameters. The URL could be truncated if the form uses a large number of parameters, or if the parameters contain large amounts of data. Also, parameters passed on the URL are visible in the address field of the browser not the best place for a password to be displayed. The alternative to the GET method is the POST method. This method packages the name/value pairs inside the body of the HTTP request, which makes for a cleaner URL and imposes no size limitations on the forms output. It is also more secure.

    Version:
    1.0.3

    Updates from previous version

  • Added HttpRead Timeout feature in case the http server is not responding. Default is 5 seconds.
  • Added HttpConnect Timeout feature. Default is 5 seconds.
    Author:
    Cnina

    Field Summary
    static int HTTP_CONNECT_TIMEOUT
               
    static int HTTP_CONNECTION_ALREADY_CONNECTED
               
    static int HTTP_CONNECTION_DISPOSED
               
    static int HTTP_CONNECTION_FAILED
               
    static int HTTP_CONNECTION_GENERAL_EXCEPTION
               
    static int HTTP_INPUT_STREAM_FAILED
               
    static int HTTP_OUTPUT_STREAM_FAILED
               
    static int HTTP_POST_SUCCESS
               
    static int HTTP_READ_TIMEOUT
               
    static int INVALID_URL
               
     
    Constructor Summary
    HttpPostWrapper(IHttpPostWrapper caller, long handle)
               
     
    Method Summary
     void dispose()
              This method will dispose of this wrapper instance.
     void doPost(java.lang.String url)
              Provide an URL to Post(or Get) an HTTP request to.
     int getConnectTimeout()
              Returns the current http connect timeout time being used in milliseconds.
     int getReadTimeout()
              Returns the current http read timeout time being used in milliseconds.
     java.util.Properties getRequestProperties()
              Retrieves the Http request properties.
     java.lang.String getRequestProperty(java.lang.String name)
              Retrieves the associated property value.
     void setConnectTimeout(int time)
              Sets the http connect timeout in milliseconds.
     void setReadTimeout(int time)
              Sets the http read timeout in milliseconds.
     void setRequestProperties(java.util.Properties props)
              Sets the HTTP request properties (also known as HTTP connection headers).
     void setRequestProperty(java.lang.String name, java.lang.String value)
              Sets the HTTP request property (also known as HTTP connection headers).
     void UsePost(boolean state)
              Sets the Request method.
     
    Methods inherited from class java.lang.Object
    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
     

    Field Detail

    HTTP_POST_SUCCESS

    public static final int HTTP_POST_SUCCESS
    See Also:
    Constant Field Values

    INVALID_URL

    public static final int INVALID_URL
    See Also:
    Constant Field Values

    HTTP_CONNECTION_FAILED

    public static final int HTTP_CONNECTION_FAILED
    See Also:
    Constant Field Values

    HTTP_OUTPUT_STREAM_FAILED

    public static final int HTTP_OUTPUT_STREAM_FAILED
    See Also:
    Constant Field Values

    HTTP_INPUT_STREAM_FAILED

    public static final int HTTP_INPUT_STREAM_FAILED
    See Also:
    Constant Field Values

    HTTP_CONNECTION_ALREADY_CONNECTED

    public static final int HTTP_CONNECTION_ALREADY_CONNECTED
    See Also:
    Constant Field Values

    HTTP_CONNECTION_GENERAL_EXCEPTION

    public static final int HTTP_CONNECTION_GENERAL_EXCEPTION
    See Also:
    Constant Field Values

    HTTP_CONNECTION_DISPOSED

    public static final int HTTP_CONNECTION_DISPOSED
    See Also:
    Constant Field Values

    HTTP_READ_TIMEOUT

    public static final int HTTP_READ_TIMEOUT
    See Also:
    Constant Field Values

    HTTP_CONNECT_TIMEOUT

    public static final int HTTP_CONNECT_TIMEOUT
    See Also:
    Constant Field Values
    Constructor Detail

    HttpPostWrapper

    public HttpPostWrapper(IHttpPostWrapper caller,
                           long handle)
    Method Detail

    UsePost

    public void UsePost(boolean state)
    Sets the Request method.

    Parameters:
    state - True if POST otherwise GET

    setReadTimeout

    public void setReadTimeout(int time)
    Sets the http read timeout in milliseconds. Use 0 for infinite or no timeout. If a negative time is specified, infinite is assumed.

    Parameters:
    time -

    getReadTimeout

    public int getReadTimeout()
    Returns the current http read timeout time being used in milliseconds.

    Returns:

    setConnectTimeout

    public void setConnectTimeout(int time)
    Sets the http connect timeout in milliseconds. Use 0 for infinite or no timeout. If a negative time is specified, infinite is assumed.

    Parameters:
    time -

    getConnectTimeout

    public int getConnectTimeout()
    Returns the current http connect timeout time being used in milliseconds.

    Returns:

    setRequestProperty

    public void setRequestProperty(java.lang.String name,
                                   java.lang.String value)
    Sets the HTTP request property (also known as HTTP connection headers).

    This property will persist, for this object instance, until changed. If the property already exists, it will be overriden.

    If a null parameter is passed it is ignored. This is an optional method. By default, this wrapper will assume a Content-Type of text/html and a Keep-Alive connection.

    Example of use: name="Content-Type" and value="text/html" (w/o the double-quotes)

    Parameters:
    name -
    value -

    setRequestProperties

    public void setRequestProperties(java.util.Properties props)
    Sets the HTTP request properties (also known as HTTP connection headers).

    These properties will persist, for this object instance, until changed. This method will override all previously established properties. If you need to change a specific property w/o affecting any of the others, use the setRequestProperty()method instead.

    If a null props parameter is passed, it is ignored. This is an optional method. By default, this wrapper will assume a Content-Type of text/html and a Keep-Alive connection.

    Parameters:
    props -

    getRequestProperty

    public java.lang.String getRequestProperty(java.lang.String name)
    Retrieves the associated property value. If the property name does not exist, an empty string is returned.

    Parameters:
    name -
    Returns:

    getRequestProperties

    public java.util.Properties getRequestProperties()
    Retrieves the Http request properties.

    The returned Properties object is never null.

    Returns:

    doPost

    public void doPost(java.lang.String url)
    Provide an URL to Post(or Get) an HTTP request to. The method used (POST or GET) is determined by setting the UsePost(boolean) method. Default is POST. The URL provided can include query parameters if needed. For example: http://192.168.103.201/lib/ajaxresults.php?cmd=nowplaying&zoneid=1

    Parameters:
    url -

    dispose

    public void dispose()
    This method will dispose of this wrapper instance. You should call this method if this Http connection is no longer needed.



    Copyright © 2008 AMX LLC. All Rights Reserved.