SophiaFramework UNIVERSE 5.3 |
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.
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; }
Copyright(c) 2002 - 2024 Sophia Cradle Incorporated All Rights Reserved. |