Class HttpForm


  • public class HttpForm
    extends java.lang.Object
    The class HttpForm is designed to post form data to an HTTP(S) server. The data being posted can be in one of three formats:
    • content type: application/x-www-form-urlencoded. posting urlencoded name-value pairs,
    • content type: MIME-type. for posting user-specified content, and
    • content type: multipart/form-data. for posting multipart data.

    Here's a snippet of code illustrating the post of name value pairs.

         HttpForm oForm = new HttpForm();
         oForm.SetEncodingType(HttpForm.PostEncodingType.URL_ENCODING);
         oForm.AddNameValuePair("fubar", "not yet");
         oForm.AddNameValuePair("name", "value");
         oForm.AddNameValuePair("submit", "now");
         oForm.Post("http://tools_build/scripts/ReadAll.asp");
         int nStatus = oForm.GetResponseCode();
         String sContentType = oForm.GetResponseType();
         String sGotten = oForm.GetResponse();
         ...
     

    Here's another snippet of code illustrating the post of data in a user-specified content type and character set.

         HttpForm oForm = new HttpForm();
         String sSent = 
             "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
             "<form>" +
             "<name first=\"Ôlêg\" last=\"ÜÇmlæt\"" +
             "</name>" +
             "</form>";
         oForm.SetEncodingType(HttpForm.PostEncodingType.USER_ENCODING);
         oForm.AddEncodedData(sSent.getBytes("UTF-8"), "text/xml", "utf-8");
         oForm.Post("http://tools_build/scripts/ReadAll.asp");
         int nStatus = oForm.GetResponseCode();
         String sContentType = oForm.GetResponseType();
         String sGotten = oForm.GetResponse();
         ...
     

    Here's another snippet of code illustrating the post of multipart data in a user-specified content type and character set.

         HttpForm oForm = new HttpForm();
         oForm.SetEncodingType(HttpForm.PostEncodingType.MULTIPART_ENCODING);
         oForm.AddMultipartData(Protocol.SectionDataOption.SECTION_CONTENT_NAME, "fubar".getBytes("US-ASCII"));
         oForm.AddMultipartData(Protocol.SectionDataOption.SECTION_CONTENT_VALUE, "not!".getBytes("US-ASCII"));
         oForm.AddMultipartData(Protocol.SectionDataOption.SECTION_END, null);
         oForm.AddMultipartData(Protocol.SectionDataOption.SECTION_CONTENT_FILE, "protocol/test.html".getBytes("US-ASCII"));
         oForm.AddMultipartData(Protocol.SectionDataOption.SECTION_END, null);
         oForm.AddMultipartData(Protocol.SectionDataOption.SECTION_CONTENT_NAME, "lotto".getBytes("US-ASCII"));
         oForm.AddMultipartData(Protocol.SectionDataOption.SECTION_CONTENT_FILE, "protocol/lotto.wsdl".getBytes("US-ASCII"));
         oForm.AddMultipartData(Protocol.SectionDataOption.SECTION_END, null);
         oForm.Post(sUrl);
         int nStatus = oForm.GetResponseCode();
         String sContentType = oForm.GetResponseType();
         String sGotten = oForm.GetResponse();
         ...
     
    Author: Mike P. Tardif
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int ChunkSize  
      static int MixedSize  
    • Constructor Summary

      Constructors 
      Constructor Description
      HttpForm()
      The default c'tor -- instantiate a HttpForm object.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addEncodedData​(byte[] encodedData, java.lang.String sContentType, java.lang.String sCharSet)
      Add the given encoded data to the form data being accumulated.
      void addHeaderData​(java.lang.String sKey, java.lang.String sValue)
      Add a key-value pair to the header data
      void addMultipartData​(Protocol.SectionDataOption eOption, byte[] value)
      Add the given multipart section to the form data being accumulated.
      void addNameValuePair​(java.lang.String name, java.lang.String value)
      Add the given (non-urlencoded) name=value pair to the form data being accumulated.
      byte[] getResponse()
      Get the data response from the last post.
      int getResponseCode()
      Get the status code response from the last post.
      java.lang.String getResponseType()
      Get the content type response from the last post.
      void post​(java.lang.String sUrl)
      Post accumulated form data to a designated URL.
      void setEncodingType​(HttpForm.PostEncodingType ePostEncodingType)
      Set encoding type.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • HttpForm

        public HttpForm()
        The default c'tor -- instantiate a HttpForm object.
    • Method Detail

      • setEncodingType

        public void setEncodingType​(HttpForm.PostEncodingType ePostEncodingType)
        Set encoding type. One of
        • URL_ENCODING for posting urlencoded name-value pairs -- (content type: application/x-www-form-urlencoded),
        • USER_ENCODING for posting user-specified content, and
        • MULTIPART_ENCODING for posting multipart data -- (content type: multipart/form-data)

        Resetting this property clears any previously accumulated form data.

        Should be called before AddData() is called.

        Parameters:
        ePostEncodingType - the encoding type
      • addNameValuePair

        public void addNameValuePair​(java.lang.String name,
                                     java.lang.String value)
        Add the given (non-urlencoded) name=value pair to the form data being accumulated. The name=value pair will be suitably urlencoded, and appended to the data being posted.

        This method can be called repeatedly to add to the form's data whenever the encoding-type is application/x-www-form-urlencoded.

        Parameters:
        name - the name
        value - the value
      • addEncodedData

        public void addEncodedData​(byte[] encodedData,
                                   java.lang.String sContentType,
                                   java.lang.String sCharSet)
        Add the given encoded data to the form data being accumulated. The encoded data will be appended to the data being posted provided the content type doesn't change.

        This method can be called repeatedly to add to the form's data.

        Parameters:
        encodedData - the encoded data to add
        sContentType - optional content type for the data
        sCharSet - optional character set for the data
      • addHeaderData

        public void addHeaderData​(java.lang.String sKey,
                                  java.lang.String sValue)
        Add a key-value pair to the header data

        This method can be called repeatedly to add to the header data.

        Parameters:
        sKey - the header type (Content-Type, charset etc.)
        sValue - the header value
      • addMultipartData

        public void addMultipartData​(Protocol.SectionDataOption eOption,
                                     byte[] value)
        Add the given multipart section to the form data being accumulated.

        This method can be called repeatedly to add to the form's data whenever the encoding-type is multipart/form-data. Each call creates a separate section of multipart data.

        The section description will allow the user to specify any one of:

        • the content name,
        • the content type,
        • the content origin (memory or file),
        • the content length.
        for each section.
        Parameters:
        eOption - the section type
        value - the value
      • post

        public void post​(java.lang.String sUrl)
        Post accumulated form data to a designated URL.
        Parameters:
        sUrl - The designated URL
      • getResponse

        public byte[] getResponse()
        Get the data response from the last post. This excludes all response headers.

        Calling this method is only meaningful after a Post().

        Returns:
        the response data
      • getResponseCode

        public int getResponseCode()
        Get the status code response from the last post.

        Calling this method is only meaningful after a Post().

        Returns:
        the response code
      • getResponseType

        public java.lang.String getResponseType()
        Get the content type response from the last post.

        Calling this method is only meaningful after a Post().

        Returns:
        the response type