PrevNextUpHome SophiaFramework UNIVERSE 5.3

18.3. SFXHTTPConnection Class

The SFXHTTPConnection class provides the functions necessary for the HTTP and HTTPS communication similar to those of com.nttdocomo.io.HttpConnection class implemented in the NTT DoCoMo development environment for Mobile Java applications( DoJa).

Data can be received from / sent to the web server using the SFXHTTPConnection class as follows:

How to use the SFXHTTPConnection class

The HTTP client can be implemented by taking the following procedures with the SFXHTTPConnection class.

  1. Create the SFXHTTPConnection instance, that is, the HTTP connection.
  2. Open the HTTP connection using the SFXHTTPConnection::Open function.
  3. Set the HTTP request method(default: GET method) using the SFXHTTPConnection::SetMethod function. And set data to the HTTP request body through the output stream obtained by the SFXHTTPConnection::GetStreamWriter function. Set the HTTP request header using the SFXHTTPConnection::SetRequestHeader function, if necessary. (* The HTTP request body can be set using the SFXHTTPConnection::SetRequestContent function.)
  4. In case of the HTTPS connection, set the trust mode(default: SSL_TRUST_MODE_FAIL) using the SFXHTTPConnection::SetTrustMode function.
  5. Connect to the Web server using the SFXHTTPConnection::Connect function, in which set the URL to be connected to and register the callback function notified of the result of HTTP connection request.
  6. The callback function above will be called after notified of the result of connecting to the Web server. (* The execution result(error code) of connecting to the Web server will be passed to the callback function through the 1st argument. If the error code is "SFERR_NO_ERROR", connecting to the Web server is succeeded and the HTTP response is obtained.)
  7. After checking the value of HTTP status code using the SFXHTTPConnection::GetResultCode function, read the HTTP response body through the input stream obtained by the SFXHTTPConnection::GetStreamReader function. The HTTP response header can be gotten using the SFXHTTPConnection::GetResponseHeader function. (* The HTTP response body can be gotten using the SFXHTTPConnection::GetResponseContent function.)
  8. Repeat the above steps from 2. to 7. until no other data needs to be written or read.
  9. Finally, close HTTP connection using the SFXHTTPConnection::Close function.

Reference: Network: HTTP Connection

Example 18.3. HTTP communication through the stream

// The _http variable is defined as class member variable since used in the callback function
class MyClass {
private:
    SFXHTTPConnection         _http;
    SFXAnsiStringStreamReader _reader;
    SFXAnsiString             _string;
public:
    Void Start(Void);
    XALLBACK_DECLARE_SFXHTTPCONNECTION(OnConnect)
    XALLBACK_DECLARE_SFXANSISTRINGSTREAMREADER(OnFetch)
};

Void MyClass::Start(Void)
{
    SFXAnsiStringStreamWriter writer;
    SFXAnsiString             message;
    SFCError error(SFERR_NO_ERROR);

    unused(environment);

    // HTTP connection using POST method
    if ((error = _http.Open()) == SFERR_NO_ERROR) {

        // message: content of HTTP request body to set
        message = "abcdefghijklmnopqrstuvwxyz";

        // get output stream for writing data into HTTP request body
        if ((error = _http.GetStreamWriter(message.GetLength(), &writer)) == SFERR_NO_ERROR) {

            // write data from the message variable into output stream buffer
            if ((error = writer.WriteSFXAnsiString(message)) == SFERR_NO_ERROR) {

                // write data from output stream buffer into http request body actually
                // data will not be written into http request body if Flush function is not called explicitly
                if ((error = writer.Flush()) == SFERR_NO_ERROR) {

                    // set request method to POST
                    if ((error = _http.SetMethod("POST")) == SFERR_NO_ERROR) {

                        // start to connect
#if 1
                        // in case of HTTP connection
                        // connect to the Web server
                        // * the connection result will be notified to the OnConnect function
                        if ((error = _http.Connect("http://www.example.com",
                            XALLBACK_INTERNAL(OnConnect))) == SFERR_NO_ERROR) {
#else
                        // in case of HTTPS connection
                        // set trust mode if necessary
                        _http.SetTrustMode(SSL_TRUST_MODE_FAIL);
                        // connect to the Web server
                        // * the connection result will be notified to the OnConnect function
                        if ((error = _http.Connect("https://www.example.com",
                            XALLBACK_INTERNAL(OnConnect))) == SFERR_NO_ERROR) {
#endif
                            TRACE("> connecting...");
                        }
                    }
                }
            }
        }
    }

    if (error != SFERR_NO_ERROR) {

        // if an error occurs
        // close the HTTP connection
        _http.Close();
    }
    return;
}

// callback function notified of the connection result
XALLBACK_IMPLEMENT_SFXHTTPCONNECTION(MyClass, OnConnect, error)
{
    TRACE("> connected... : %d", error);

    if (error == SFERR_NO_ERROR) {

        // examine response code
        TRACE("> result code : %d", _http.GetResultCode());

        if (_http.GetResultCode() == 200) {

            // get input stream for reading data from HTTP response body
            if ((error = _http.GetStreamReader(1024, &_reader)) == SFERR_NO_ERROR) {

                // read data from HTTP request body into input stream buffer
                // the fetch result will be notified to the OnFetch function
                if ((error = _reader.Fetch(XALLBACK_INTERNAL(OnFetch))) == SFERR_NO_ERROR) {

                    TRACE("> fetching...");
                }
            }
        }
        else {
            error = SFERR_INVALID_STATE;
        }
    }

    if (error != SFERR_NO_ERROR) {

        // if an error occurs
        // close the HTTP connection
        _http.Close();
    }
    return;
}

// callback function notified of the fetch result
XALLBACK_IMPLEMENT_SFXANSISTRINGSTREAMREADER(MyClass, OnFetch, error)
{
    SFXAnsiString string;

    TRACE("> fetched... : %d", error);

    if (error == SFERR_NO_ERROR) {

        // read data from input stream buffer into the string variable
        if ((error = _reader.ReadSFXAnsiString(&string)) == SFERR_NO_ERROR) {

            // add the string variable to the _string variable
            if ((error = _string.Add(string)) == SFERR_NO_ERROR) {

                // check whether or not data is still remaining
                if (_reader.Ends()) {

                    // since all data have been read, display them on BREW Output Window
                    TRACE("--------");
                    TRACE("%s", _string.GetCString());
                    TRACE("--------");

                    // release resources explicitly
                    // release input stream for reading data from HTTP response body
                    _reader.Release();
                    // close the HTTP connection
                    _http.Close();
                }
                else {

                    // read data from HTTP request body into input stream buffer
                    // the fetch result will be notified to the OnFetch function
                    error = _reader.Fetch(XALLBACK_INTERNAL(OnFetch));
                }
            }
        }
    }
	
    if (error != SFERR_NO_ERROR) {

        // if an error occurs
        // release input stream for reading data from HTTP response body
        _reader.Release();
        // close the HTTP connection
        _http.Close();
    }
    return;
}