Class StandardHttpClient

  • Direct Known Subclasses:
    CompressedHttpClient

    public class StandardHttpClient
    extends java.lang.Object
    Provides client-side HTTP communication
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.lang.String auth  
      protected java.lang.String host  
      protected java.lang.String hostname  
      Hashtable htHeader
      The header contents, with both key and value lowercased.
      boolean keepAlive
      Sets the keep-alive flag.
      protected int port  
      protected BufferedStream reader  
      protected java.lang.StringBuffer sb  
      protected Socket socket  
      protected java.lang.String uri  
    • Constructor Summary

      Constructors 
      Constructor Description
      StandardHttpClient​(java.lang.String hostname, int port, java.lang.String uri)  
      StandardHttpClient​(java.lang.String hostname, int port, java.lang.String uri, int openTimeout, int readTimeout, int writeTimeout)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void checkResponse()
      Checks if the response of the server has status 200
      protected void closeConnection()
      Terminates the server connection
      java.lang.String execute​(byte[] requestBody)
      Executes a HTTP request to the connected server
      byte[] executeReturnBytes​(byte[] requestBody)
      Executes a HTTP request to the connected server.
      protected void parseHeader()
      Places all the returning values frm the header in a hashtable for easy access.
      protected java.lang.StringBuffer privateReadResponse()
      Used internally by readResponse and readResponseBytes.
      java.lang.String readLine()
      Reads a line from the socket, retrying up to four times, with a 250ms delay between the tries.
      protected java.lang.String readResponse()
      Reads all the lines and place them in a single contiguous String.
      protected java.lang.Object readResponseBytes()
      Reads all the lines and place them in a single contiguous String.
      void setBasicAuthentication​(java.lang.String user, java.lang.String password)
      Base64 encodes the username and password given for basic server authentication
      protected void writeRequest​(byte[] requestBody, int len)
      Writes the header followed by the given request body
      protected java.lang.StringBuffer writeRequestHeader​(int requestLength)  
      • Methods inherited from class java.lang.Object

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

      • port

        protected int port
      • uri

        protected java.lang.String uri
      • host

        protected java.lang.String host
      • auth

        protected java.lang.String auth
      • socket

        protected Socket socket
      • hostname

        protected java.lang.String hostname
      • htHeader

        public Hashtable htHeader
        The header contents, with both key and value lowercased.
      • keepAlive

        public boolean keepAlive
        Sets the keep-alive flag. If this is false, the connection to the server will be closed at each execution.
      • sb

        protected java.lang.StringBuffer sb
    • Constructor Detail

      • StandardHttpClient

        public StandardHttpClient​(java.lang.String hostname,
                                  int port,
                                  java.lang.String uri,
                                  int openTimeout,
                                  int readTimeout,
                                  int writeTimeout)
                           throws UnknownHostException,
                                  XmlRpcException
        Parameters:
        hostname - The server address to connect to
        port - The port on the server we want to connect to
        uri - The connecting URI. Defaults to "/RPC2"
        openTimeout -
        readTimeout -
        writeTimeout -
        Throws:
        XmlRpcException - If the connection to the server could not be made
        UnknownHostException
      • StandardHttpClient

        public StandardHttpClient​(java.lang.String hostname,
                                  int port,
                                  java.lang.String uri)
                           throws UnknownHostException,
                                  XmlRpcException
        Parameters:
        hostname - The server address to connect to
        port - The port on the server we want to connect to
        uri - The connecting URI. Defaults to "/RPC2"
        Throws:
        XmlRpcException - If the connection to the server could not be made
        UnknownHostException
    • Method Detail

      • setBasicAuthentication

        public void setBasicAuthentication​(java.lang.String user,
                                           java.lang.String password)
        Base64 encodes the username and password given for basic server authentication
        Parameters:
        user - The username for the server. Passing null disables authentication.
        password - The password for the username account on the server. Passing null disables authentication.
      • writeRequest

        protected void writeRequest​(byte[] requestBody,
                                    int len)
                             throws IOException
        Writes the header followed by the given request body
        Throws:
        IOException
      • readLine

        public java.lang.String readLine()
                                  throws IOException
        Reads a line from the socket, retrying up to four times, with a 250ms delay between the tries.
        Throws:
        IOException
        Since:
        SuperWaba 5.68
      • parseHeader

        protected void parseHeader()
                            throws IOException
        Places all the returning values frm the header in a hashtable for easy access. Already parses the keepAlive flag.
        Throws:
        IOException
      • readResponse

        protected java.lang.String readResponse()
                                         throws XmlRpcException,
                                                IOException
        Reads all the lines and place them in a single contiguous String.
        Parameters:
        returnAsBytes - If true, the result can be casted to byte[], otherwise, it can be casted to String.
        Throws:
        IOException
        XmlRpcException
      • readResponseBytes

        protected java.lang.Object readResponseBytes()
                                              throws XmlRpcException,
                                                     IOException
        Reads all the lines and place them in a single contiguous String.
        Parameters:
        returnAsBytes - If true, the result can be casted to byte[], otherwise, it can be casted to String.
        Throws:
        IOException
        XmlRpcException
      • execute

        public java.lang.String execute​(byte[] requestBody)
                                 throws XmlRpcException,
                                        IOException
        Executes a HTTP request to the connected server
        Parameters:
        requestBody - The contents of the HTTP request. Headers are added appropriately by this method
        Throws:
        XmlRpcException - If the server returns a status code other than 200 OK
        IOException
      • executeReturnBytes

        public byte[] executeReturnBytes​(byte[] requestBody)
                                  throws XmlRpcException,
                                         IOException
        Executes a HTTP request to the connected server.
        Parameters:
        requestBody - The contents of the HTTP request. Headers are added appropriately by this method
        Throws:
        XmlRpcException - If the server returns a status code other than 200 OK
        IOException
        Since:
        TotalCross 1.23
      • closeConnection

        protected void closeConnection()
                                throws IOException
        Terminates the server connection
        Throws:
        IOException
      • writeRequestHeader

        protected java.lang.StringBuffer writeRequestHeader​(int requestLength)