PrevNextUpHome BREW C++ Class Library & GUI Framework & XML Middleware : SophiaFramework 4.1
SFXHTTPConnection
Class for the HTTP/HTTPS communication.
#include <SFXHTTPConnection.h.hpp>
class SFXHTTPConnection;
SFMTYPEDEFCLASS(SFXHTTPConnection)

Inheritance diagram

 Collaboration diagram of SFXHTTPConnectionClass

Description

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

[Note] Note
The SFXHTTPConnection class is implemented using the IWeb, IWebResp, and IX509Chain interfaces of BREW APIs.

How to use the HTTP/HTTPS connection

  1. Create the HTTP/HTTPS connection(instance of SFXHTTPConnection class).
  2. Open the HTTP/HTTPS 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.)
  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 that will receive the notification of connecting to the Web server.
  6. The callback function above will be called after notified 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.
  8. Repeat the above steps from 2. to 7. until no other data needs to be sent or received.
  9. Finally, close HTTP/HTTPS connection using the SFXHTTPConnection::Close function.

Example 472. Sample code for the HTTP/HTTPS communication

// define _http, instance of SFXHTTPConnection class, as member variable
class MyClass {
private:
    SFXHTTPConnection _http;
    SFXAnsiStringStreamReader _reader;
    SFXAnsiString _receivedString;
public:
    Void Start(Void);
    CALLBACK_DECLARE_SFXHTTPCONNECTION(OnConnect)
    CALLBACK_DECLARE_SFXANSISTRINGSTREAMREADER(OnFetch)
};

Void MyClass::Start(Void)
{
    SFCError error(SFERR_NO_ERROR);

    // initialize HTTP connection
    if ((error = _http.Open()) == SFERR_NO_ERROR) {
    
        // set HTTP request method to "GET"
        // if this setting is obmitted, "GET" is set by default
        _http.SetMethod("GET");

        // start to connect 
#if 1
        // HTTP connection
        // connection establishment will be notified to OnConnect function
        if ((error = _http.Connect("http://www.example.com/",
                         CALLBACK_FUNCTION(OnConnect))) == SFERR_NO_ERROR) {
#else
        // HTTPS connection
        // set verification mode if needed
        // connection establishment will be notified to OnConnect function
        _http.SetTrustMode(SSL_TRUST_MODE_FAIL);
        if ((error = _http.Connect("https://www.example.com/",
                         CALLBACK_FUNCTION(OnConnect))) == SFERR_NO_ERROR) {
#endif
            ...
        }
    }

    if (error != SFERR_NO_ERROR) {
        // if error occurs
        _http.Close();
    }
    return;
}

// callback function notified of connection establishment
CALLBACK_IMPLEMENT_SFXHTTPCONNECTION(MyClass, OnConnect, error)
{
    SFXPropertyConstPtr header;
    SInt16 i;

    if (error == SFERR_NO_ERROR) {
        
        // display parameters

        // get HTTP status code
        TRACE("result code = %d", _http.GetResultCode());
        // get Content-Length field of HTTP response header
        TRACE("content length = %d", _http.GetLength()); 
        // get Content-Type field of HTTP response header
        TRACE("content type = %s", _http.GetType().GetCString());
        // get Content-Encoding field of HTTP response header
        TRACE("content encoding = %s", _http.GetEncoding().GetCString());
        // get Date field of HTTP response header
        TRACE("date = %s", _http.GetDate().Format("YYYY/MM/DD hh:mm:ss").GetCString());
        // get Expires field of HTTP response header
        TRACE("expires = %s", _http.GetExpires().Format("YYYY/MM/DD hh:mm:ss").GetCString());
        // get Last-Modified field of HTTP response header
        TRACE("last modified = %s", _http.GetLastModified().Format("YYYY/MM/DD hh:mm:ss").GetCString());

        
        // get HTTP response header
        header = &_http.GetResponseHeader();
        
        // display HTTP response header
        for (i = 0; i < header->GetSize(); ++i) {
            TRACE("%s: %s", header->GetKey(i).GetCString(), header->GetValue(i).GetCString());
        }

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

            // get input stream for receiving data
            if ((error = _http.GetStreamReader(1024, &_reader))
                == SFERR_NO_ERROR) {

                // receive data actually (completion of receiving data will be notified to OnFetch function)
                if ((error = _reader.Fetch(CALLBACK_FUNCTION(OnFetch)))
                    == SFERR_NO_ERROR) {
                    ...
                }
            }
        }
        else {
            error = SFERR_INVALID_STATE;
        }
    }

    if (error != SFERR_NO_ERROR) {
        // if error occurs
       _http.Close();
    }
    return;
}

// callback function notified of completion of receiving data actually
CALLBACK_IMPLEMENT_SFXANSISTRINGSTREAMREADER(MyClass, OnFetch, error)
{
    SFXAnsiString string;

    if (error == SFERR_NO_ERROR) {

        // get data from input stream
        if ((error = _reader.ReadSFXAnsiString(&string)) == SFERR_NO_ERROR) {

            // combine fragmented data
            if ((error = _receivedString.Add(string)) == SFERR_NO_ERROR) {

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

                    // display all data received
                    TRACE("--------");
                    TRACE("%s", _receivedString.GetCString());
                    TRACE("--------");

                    // release resources explicitly
                    _reader.Release();
                    _http.Close();
                }
                else {

                    // receive data actually ( completion of receiving data will be notified to OnFetch function )
                    error = _reader.Fetch(CALLBACK_FUNCTION(OnFetch));
                }
            }
        }
    }

    if (error != SFERR_NO_ERROR) {
        // if error occurs
        _reader.Release();
        _http.Close();
    }
    return;
}

Limitation

BREW 2.0 IWeb Interface limitation: data exceeding 1536 bytes cannot be sent.

BREW 2.0 IWeb Interface limitation: data exceeding 64K bytes cannot be sent.

Reference

BREW API IWeb | BREW API IWebResp | BREW API IX509Chain | SFXTCPSocket | HTTP connection

Member

Constructor/Destructor
SFXHTTPConnection( Void )
Constructor of SFXHTTPConnection class.
~SFXHTTPConnection( Void )
Destructor of SFXHTTPConnection class.
Public Functions
Void Cancel( Void )
Cancel the HTTP/HTTPS communication.
SFCError Clear( Void )
Initialize the internal variables.
Void Close( Void )
Close the HTTP/HTTPS connection.
SFCError Connect( SFXAnsiStringConstRef url , CallbackSPP spp , VoidPtr reference )
Connect to the Web server.
SFXDate GetDate( Void )
Get the Date field of HTTP response header.
SFXAnsiStringConstRef GetEncoding( Void )
Get the Content-Encoding field of HTTP response header.
SFXDate GetExpires( Void )
Get the Expires field of HTTP response header.
SFXDate GetLastModified( Void )
Get the Last-Modified field of HTTP response header.
SInt32 GetLength( Void )
Get the Content-Length field of HTTP response header.
SFXAnsiStringConstRef GetMethod( Void )
Get the HTTP request method.
SFXSocketAddressConstRef GetProxyServer( Void )
Get the proxy server.
SFBSourceSmpConstRef GetRequestContent( Void )
Get the instance of SFBSource class that manages data to be sent.
UInt32 GetRequestFlag( Void )
Get the HTTP request flag.
SFXAnsiStringConstRef GetRequestHeader( SFXAnsiStringConstRef key )
Get the HTTP request header.
SFXPropertyConstRef GetRequestHeader( Void )
Get the HTTP request header.
SFBSourceSmpConstRef GetResponseContent( Void )
Get the instance of SFBSource class that manages data to be received.
UInt32 GetResponseFlag( Void )
Get the HTTP response flag.
SFXAnsiStringConstRef GetResponseHeader( SFXAnsiStringConstRef key )
Get the HTTP response header.
SFXPropertyConstRef GetResponseHeader( Void )
Get the HTTP response header.
SInt32 GetResultCode( Void )
Get the HTTP status code.
SFBWebSmpConstRef GetSFBWeb( Void )
Get the instance of SFBWeb class that is internally used.
SFBWebRespSmpConstRef GetSFBWebResp( Void )
Get the instance of SFBWebResp class that is internally used.
SFCError GetStreamReader( UInt32 size , SFXStreamReaderPtr result )
GetStreamReader( SFXStreamReaderPtr result )
Get the input stream for reading data from the HTTP/HTTPS connection.
SFCError GetStreamWriter( UInt32 size , SFXStreamWriterPtr result )
GetStreamWriter( SFXStreamWriterPtr result )
Get the output stream for writing data to the HTTP/HTTPS connection.
UInt32 GetTrustMode( Void )
Get the SSL trust mode.
SFXAnsiStringConstRef GetType( Void )
Get the Content-Type field of HTTP request header.
SFXAnsiStringConstRef GetUserAgent( Void )
Get the user agent.
SFCError Open( AEECLSID id = AEECLSID_WEB )
Open the HTTP/HTTPS connection.
SFCError SetMethod( SFXAnsiStringConstRef param )
Set the HTTP request method.
SFCError SetProxyServer( SFXSocketAddressConstRef param )
Set the proxy server.
SFCError SetRequestContent( SFXStorageConstRef param )
SetRequestContent( SFBSourceSmpConstRef param )
Set the instance of SFBSource class that manages data to be sent.
SFCError SetRequestFlag( UInt32 param )
Set the HTTP request flag.
SFCError SetRequestHeader( SFXAnsiStringConstRef key , SFXAnsiStringConstRef value )
Set the HTTP request header.
SFCError SetTrustMode( UInt32 param )
Set the SSL trust mode.
SFCError SetUserAgent( SFXAnsiStringConstRef param )
Set the user agent.
Types
CallbackSPP
Prototype of the callback function.

SFXHTTPConnection::SFXHTTPConnection
Constructor of SFXHTTPConnection class.
[ public, explicit ]
SFXHTTPConnection(Void);

Description

Initialize member variables


SFXHTTPConnection::~SFXHTTPConnection
Destructor of SFXHTTPConnection class.
[ public ]
~SFXHTTPConnection(Void);

Description

If the connection is in the open status, the SFXHTTPConnection::Close function will be called internally, and all the resources are released.


SFXHTTPConnection::Cancel
Cancel the HTTP/HTTPS communication.
[ public ]
Void Cancel(Void);

Reference

SFXHTTPConnection::Close


SFXHTTPConnection::Clear
Initialize the internal variables.
[ public ]
SFCError Clear(Void);

Return value

  • Success : SFERR_NO_ERROR
  • If the connection has not been established : SFERR_INVALID_STATE
  • If insufficient memory : SFERR_NO_MEMORY

Reference

SFXHTTPConnection::Cancel


SFXHTTPConnection::Close
Close the HTTP/HTTPS connection.
[ public ]
Void Close(Void);

Description

Terminate the connection to the Web server, and release all the resources that are used internally.

Reference

SFXHTTPConnection::Cancel


SFXHTTPConnection::Connect
Connect to the Web server.
[ public ]
SFCError Connect(
    SFXAnsiStringConstRef url   // connect URL
    CallbackSPP spp             // callback function
    VoidPtr reference           // data passed to callback function
);

Return value

  • Success : SFERR_NO_ERROR
  • If the connection has not been established : SFERR_INVALID_STATE
  • If insufficient memory : SFERR_NO_MEMORY
  • If failed : SFERR_FAILED

Description

Destroy the streams obtained by the SFXHTTPConnection::GetStreamReader and SFXHTTPConnection::GetStreamWriter functions and the HTTP response header obtained in the previous connection.

[Caution] Errors while connecting

The return value does not include the errors while connecting to the Web server, which are notified to the callback function.

Example

// define _http, instance of SFXHTTPConnection class, as member variable
class MyClass {
private:
    SFXHTTPConnection _http;
public:
    Void Start(Void);                               // start HTTP connection
    CALLBACK_DECLARE_SFXHTTPCONNECTION(OnConnect)   // callback function declaration
};

// start HTTP connection
Void MyClass::Start(Void)
{
    SFCError error(SFERR_NO_ERROR);

    if ((error = _http.Open()) == SFERR_NO_ERROR) {   // open HTTP connection
    
        _http.SetMethod("GET");  // set HTTP request method to "GET"

        // connect to Web server
        // connection establishment will be notified to OnConnect function
        if ((error = _http.Connect("http://www.example.com/",
                         CALLBACK_FUNCTION(OnConnect))) == SFERR_NO_ERROR) {
           ...
        }else{
            // if error occurs, close HTTP connection
            _http.Close(); 
        }
        
    }

    if (error != SFERR_NO_ERROR) {  
        // if error occurs
        ...
    }
    return;
}
// callback function notified of connection establishment
CALLBACK_IMPLEMENT_SFXHTTPCONNECTION(MyClass, OnConnect, error)
{
    if (error == SFERR_NO_ERROR) {
        ...
    }
}

Reference

SFXHTTPConnection::Open


SFXHTTPConnection::GetDate
Get the Date field of HTTP response header.
[ public, const ]
SFXDate GetDate(Void);

Return value

Parse the Date field of HTTP response header, and return it as an instance of SFXDate class.

Return 0 (instance of SFXDate class initialized with 0) if the HTTP response header dose not include the Date field.

Description

The SFXHTTPConnection::GetDate function returns the date and time as the number of seconds since January 6, 1980 00:00:00 (GMT). The date and time in this format is handled in the BREW helper functions: the GETJULIANDATE function and the GETTIMESECONDS function.

Example

// callback function notified of connection establishment
CALLBACK_IMPLEMENT_SFXHTTPCONNECTION(MyClass, OnConnect, error)
{
    if (error == SFERR_NO_ERROR) {

        // get Date field of HTTP response header 
        TRACE("date = %s", _http.GetDate().Format("YYYY/MM/DD hh:mm:ss").GetCString());
        ...
    }
}

SFXHTTPConnection::GetEncoding
Get the Content-Encoding field of HTTP response header.
[ public, const ]
SFXAnsiStringConstRef GetEncoding(Void);

Example

// callback function notified of connection establishment
CALLBACK_IMPLEMENT_SFXHTTPCONNECTION(MyClass, OnConnect, error)
{
    if (error == SFERR_NO_ERROR) {

        // get Content-Encoding field of HTTP response header
        TRACE("content encoding = %s", _http.GetEncoding().GetCString()); 
        ...
    }
}

SFXHTTPConnection::GetExpires
Get the Expires field of HTTP response header.
[ public, const ]
SFXDate GetExpires(Void);

Return value

Parse the Expires field of HTTP response header, and return it as an instance of SFXDate class.

Return 0 (instance of SFXDate class initialized with 0) if if the HTTP response header dose not include the Expires field.

Description

The SFXHTTPConnection::GetExpires function returns the date and time as the number of seconds since January 6, 1980 00:00:00 (GMT). The date and time in this format is handled in the BREW helper functions: the GETJULIANDATE function and the GETTIMESECONDS function.

Example

// callback function notified of connection establishment
CALLBACK_IMPLEMENT_SFXHTTPCONNECTION(MyClass, OnConnect, error)
{
    if (error == SFERR_NO_ERROR) {

        // get Expires field of HTTP response header
        TRACE("expires = %s", _http.GetExpires().Format("YYYY/MM/DD hh:mm:ss").GetCString());
        ...
    }
}

SFXHTTPConnection::GetLastModified
Get the Last-Modified field of HTTP response header.
[ public, const ]
SFXDate GetLastModified(Void);

Return value

Parse the Last-Modified field of HTTP response header, and return it as an instance of SFXDate class.

Return 0 (instance of SFXDate class initialized with 0) if the HTTP response header dose not include the Last-Modified field.

Example

// callback function notified of connection establishment
CALLBACK_IMPLEMENT_SFXHTTPCONNECTION(MyClass, OnConnect, error)
{
    if (error == SFERR_NO_ERROR) {

        // get Last-Modified field of HTTP response header
        TRACE("last modified = %s", _http.GetLastModified().Format("YYYY/MM/DD hh:mm:ss").GetCString());
        ...
    }
}

SFXHTTPConnection::GetLength
Get the Content-Length field of HTTP response header.
[ public, const ]
SInt32 GetLength(Void);

Return value

Return the Content-Length field of HTTP response header. Return -1 if failed.

Example

// callback function notified of connection establishment
CALLBACK_IMPLEMENT_SFXHTTPCONNECTION(MyClass, OnConnect, error)
{
    if (error == SFERR_NO_ERROR) {

        // get Content-Length field of HTTP response header
        TRACE("content length = %d", _http.GetLength());
        ...
    }
}

SFXHTTPConnection::GetMethod
Get the HTTP request method.
[ public, const ]
SFXAnsiStringConstRef GetMethod(Void);

Example

// callback function notified of connection establishment
CALLBACK_IMPLEMENT_SFXHTTPCONNECTION(MyClass, OnConnect, error)
{
    if (error == SFERR_NO_ERROR) {

        // get HTTP request method
        TRACE("method = %s", _http.GetMethod().GetCString());
        ...
    }
}

Reference

SFXHTTPConnection::SetMethod


SFXHTTPConnection::GetProxyServer
Get the proxy server.
[ public, const ]
SFXSocketAddressConstRef GetProxyServer(Void);

Description

The proxy server is set as an instance of the SFXSocketAddress class.

Example

// callback function notified of connection establishment
CALLBACK_IMPLEMENT_SFXHTTPCONNECTION(MyClass, OnConnect, error)
{
    if (error == SFERR_NO_ERROR) {

        // get the proxy server
        TRACE("method = %s", _http.GetProxyServer().Get().GetCString());
        ...
    }
}

Reference

SFXHTTPConnection::SetProxyServer


SFXHTTPConnection::GetRequestContent
Get the instance of SFBSource class that manages data to be sent.
[ public, const ]
SFBSourceSmpConstRef GetRequestContent(Void);

Description

[Caution] Caution

When using the SFXHTTPConnection::GetRequestContent function, both the SFXHTTPConnection::GetStreamWriter function and SFXHTTPConnection::GetStreamReader function cannot be used.

Reference

SFXHTTPConnection::SetRequestContent


SFXHTTPConnection::GetRequestFlag
Get the HTTP request flag.
[ public, const ]
UInt32 GetRequestFlag(Void);

Return value

Detailed information on receiving the HTTP request can be obtained as the following logical sum of flags:

  • WEBREQUEST_REDIRECT: The redirect is automatically processed.(Not Implemented)
  • WEBREQUEST_NOCOOKIES: The header is passed without processing the cookies.(Not Implemented)
  • WEBREQUEST_NOCACHE: When using the proxy, send "Pragma: no-cache".(Not Implemented)
  • WEBREQUEST_NOKEEPALIVE: The KEEP ALIVE status is invalidated.
  • WEBREQUEST_FORCENEWCONN: New connection is forced.
  • WEBREQUEST_NOWAITCONN: Without waiting for the reusable connection, try to connect newly.
  • WEBREQUEST_HTTPBOGUSCRLF: To avoid the bugs on the CERN Webserver, append an extra CRLF.

Description

The SFXHTTPConnection::GetRequestFlag function returns the detailed communication settings which are passed to IWEB_GetResponse function used during communication internally.

Reference

BREW API IWEB_GetResponse | SFXHTTPConnection::SetRequestFlag


SFXHTTPConnection::GetRequestHeader
Get the HTTP request header.
[ public, const ]
SFXAnsiStringConstRef GetRequestHeader(
    SFXAnsiStringConstRef key   // field name
);
[ public, const ]
SFXPropertyConstRef GetRequestHeader(Void);

Return value

Return an empty property (empty string) if the connection has not been established.

Reference

SFXHTTPConnection::SetRequestHeader


SFXHTTPConnection::GetResponseContent
Get the instance of SFBSource class that manages data to be received.
[ public, const ]
SFBSourceSmpConstRef GetResponseContent(Void);

Description

[Caution] Caution

When using SFXHTTPConnection::GetResponseContent function, both the SFXHTTPConnection::GetStreamWriter function and SFXHTTPConnection::GetStreamReader function cannot be used.


SFXHTTPConnection::GetResponseFlag
Get the HTTP response flag.
[ public, const ]
UInt32 GetResponseFlag(Void);

Return value

Detailed information on receiving the HTTP response can be obtained as the following logical sum of flags:

  • WEBRESPONSE_REDIRECTED: Redirect is processed by the IWeb interface. Not implemented in BREW 2.1.
  • WEBRESPONSE_COOKIES: Cookies are transmitted with the HTTP request. Not implemented in BREW 2.1.
  • WEBRESPONSE_CACHED: Data is obtained from the local cache. Network communication is not performed. Not implemented in BREW 2.1.
  • WEBRESPONSE_KEEPALIVE: The connection is in the KEEP ALIVE status.
  • WEBRESPONSE_HTTP09: The HTTP 0.9 response is receieved. No header found. It is processed as status code 200.
  • WEBRESPONSE_LOCAL: This is the content created locally. Network communication is not performed.
  • WEBRESPONSE_PROXIED: Data is received via the proxy.
  • WEBRESPONSE_AUTHBASIC: Information on the BASIC authentication is sent. Not implemented in BREW 2.1.

Description

The SFXHTTPConnection::GetResponseFlag function returns the return value of the IWEBRESP_GetOpt function where WEBOPT_FLAGS is specified in the IWebResp interface internally.

Reference

BREW API IWEBRESP_GetOpt | SFXHTTPConnection::SetRequestFlag


SFXHTTPConnection::GetResponseHeader
Get the HTTP response header.
[ public, const ]
SFXAnsiStringConstRef GetResponseHeader(
    SFXAnsiStringConstRef key   // field name
);
[ public, const ]
SFXPropertyConstRef GetResponseHeader(Void);

Return value

Return an empty string if the connection has not been established.

Example

// callback function notified of connection establishment
CALLBACK_IMPLEMENT_SFXHTTPCONNECTION(MyClass, OnConnect, error)
{
    SFXPropertyConstPtr header;
    SInt16 i;

    if (error == SFERR_NO_ERROR) {

        // get HTTP response header
        header = &_http.GetResponseHeader(); 
        
        // display HTTP response header information
        for (i = 0; i < header->GetSize(); ++i) {
            TRACE("%s: %s", header->GetKey(i).GetCString(), header->GetValue(i).GetCString());
        }
    }
}

SFXHTTPConnection::GetResultCode
Get the HTTP status code.
[ public, const ]
SInt32 GetResultCode(Void);

Return value

Return the HTTP status code. Return -1 if the connection is not established.

Description

The SFXHTTPConnection::GetResultCode returns a negative value like the nCode field of BREW API WebRespInfo when a communication error occurs.

The error code of BREW can be obtained by passing the return value of this function to WEB_ERROR_MAP().

Example

// callback function notified of connection establishment
CALLBACK_IMPLEMENT_SFXHTTPCONNECTION(MyClass, OnConnect, error)
{
    if (error == SFERR_NO_ERROR) {
        ...
        if (_http.GetResultCode() == 200) { 
        // if HTTP status code is 200 (success)
            ...
        }
    }
}

SFXHTTPConnection::GetSFBWeb
Get the instance of SFBWeb class that is internally used.
[ public, const ]
SFBWebSmpConstRef GetSFBWeb(Void);

Reference

SFXHTTPConnection::GetSFBWebResp


SFXHTTPConnection::GetSFBWebResp
Get the instance of SFBWebResp class that is internally used.
[ public, const ]
SFBWebRespSmpConstRef GetSFBWebResp(Void);

Reference

SFXHTTPConnection::GetSFBWeb


SFXHTTPConnection::GetStreamReader
Get the input stream for reading data from the HTTP/HTTPS connection.
[ public ]
SFCError GetStreamReader(
    UInt32 size                 // buffer size
    SFXStreamReaderPtr result   // pointer to the input stream
);
[ public ]
SFCError GetStreamReader(
    SFXStreamReaderPtr result   // pointer to the input stream
);

Return value

  • Success : SFERR_NO_ERROR
  • If argument is null : SFERR_INVALID_PARAM
  • If connection has not been established : SFERR_INVALID_STATE
  • If insufficient memory : SFERR_NO_MEMORY

Description

The buffer size of the input stream can be specified. Use the SFXBinaryStreamReader, SFXAnsiStringStreamReader, or SFXWideStringStreamReader class for the input stream properly according to the type of data to read.

[Caution] Caution

When using the SFXHTTPConnection::GetStreamReader function, the SFXHTTPConnection::GetRequestContent, SFXHTTPConnection::GetResponseContent, and SFXHTTPConnection::SetRequestContent functions cannot be used.

Example

// define _http, instance of SFXHTTPConnection class, as member variable
class MyClass {
private:
    SFXHTTPConnection _http;
    SFXAnsiStringStreamReader _reader;
public:
    Void Start(Void);
    CALLBACK_DECLARE_SFXHTTPCONNECTION(OnConnect)
    CALLBACK_DECLARE_SFXANSISTRINGSTREAMREADER(OnFetch)
};

// callback function notified of connection establishment
CALLBACK_IMPLEMENT_SFXHTTPCONNECTION(MyClass, OnConnect, error)
{
    if (error == SFERR_NO_ERROR) {
        ...
        // if HTTP status code is 200 (success)
        if (_http.GetResultCode() == 200) {

            // get input stream for receiving data
            if ((error = _http.GetStreamReader(1024, &_reader)) == SFERR_NO_ERROR) {

                // receive data from HTTP connection into the stream actually
                if ((error = _reader.Fetch(CALLBACK_FUNCTION(OnFetch))) == SFERR_NO_ERROR) {
                    ...
                }
            }
        }
        else {
            error = SFERR_INVALID_STATE;
        }
    }

    if (error != SFERR_NO_ERROR) {
        // if error occurs
       _http.Close();
    }
    return;
}
// callback function notified of completion of receiving data actually
CALLBACK_IMPLEMENT_SFXANSISTRINGSTREAMREADER(MyClass, OnFetch, error)
{
    if (error == SFERR_NO_ERROR) {
        ...
    }
}

Reference

SFXHTTPConnection::GetStreamWriter | SFXBinaryStreamReader | SFXAnsiStringStreamReader | SFXWideStringStreamReader


SFXHTTPConnection::GetStreamWriter
Get the output stream for writing data to the HTTP/HTTPS connection.
[ public ]
SFCError GetStreamWriter(
    UInt32 size                 // buffer size
    SFXStreamWriterPtr result   // pointer to the output stream
);
[ public ]
SFCError GetStreamWriter(
    SFXStreamWriterPtr result   // pointer to the output stream
);

Return value

  • Success : SFERR_NO_ERROR
  • If argument is null : SFERR_INVALID_PARAM
  • If connection has not been established : SFERR_INVALID_STATE
  • If insufficient memory : SFERR_NO_MEMORY

Description

The buffer size of the output stream can be specified. Use the SFXBinaryStreamWriter, SFXAnsiStringStreamWriter, or SFXWideStringStreamWriter class for the output stream properly according to the written data type

[Caution] Caution

When using the SFXHTTPConnection::GetStreamWriter function, the SFXHTTPConnection::GetRequestContent function, SFXHTTPConnection::GetResponseContent function, and SFXHTTPConnection::SetRequestContent function cannot be used.

Example

// define _http, instance of SFXHTTPConnection class, as member variable
class MyClass {
private:
    SFXHTTPConnection _http;
public:
    Void Start(Void);
    CALLBACK_DECLARE_SFXHTTPCONNECTION(OnConnect)
};

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

    // open HTTP connection
    if ((error = _http.Open()) == SFERR_NO_ERROR) {

        // data to be sent
        send = "abcdefghijklmnopqrstuvwxyz";

        // get output stream for sending data
        if ((error = _http.GetStreamWriter(send.GetLength(), &writer)) == SFERR_NO_ERROR) {

            // write data to output stream
            if ((error = writer.WriteSFXAnsiString(send)) == SFERR_NO_ERROR) {

                // send data through output stream actually
                if ((error = writer.Flush()) == SFERR_NO_ERROR) {
                    
                    // set HTTP request method to POST
                    if ((error = _http.SetMethod("POST")) == SFERR_NO_ERROR) {  

                        if ((error = _http.Connect("http://www.example.com", CALLBACK_FUNCTION(OnConnect))) == SFERR_NO_ERROR) {   // connect
                            ...
                        }
                    }
                }
            }
        }
    }

    if (error != SFERR_NO_ERROR) {  
       // if error occurs
       _http.Close();
    }
    return;
}

// callback function notified of connection establishment
CALLBACK_IMPLEMENT_SFXHTTPCONNECTION(MyClass, OnConnect, error)
{
    if (error == SFERR_NO_ERROR) {
        ....
    }
    return;
}

Reference

SFXHTTPConnection::GetStreamReader | SFXBinaryStreamWriter | SFXAnsiStringStreamWriter | SFXWideStringStreamWriter


SFXHTTPConnection::GetTrustMode
Get the SSL trust mode.
[ public, const ]
UInt32 GetTrustMode(Void);

Description

The SSL trust mode can be one of the following values.

  • SSL_TRUST_MODE_FAIL
  • SSL_TRUST_MODE_CHECK
  • SSL_TRUST_MODE_IGNORE
  • SSL_TRUST_MODE_ALWAYS
[Note] Note
Detailed information: ISSL_NegotiateV in the BREW API Reference

Reference

BREW API ISSL_NegotiateV | SFXHTTPConnection::SetTrustMode


SFXHTTPConnection::GetType
Get the Content-Type field of HTTP request header.
[ public, const ]
SFXAnsiStringConstRef GetType(Void);

Example

// callback function notified of connection establishment
CALLBACK_IMPLEMENT_SFXHTTPCONNECTION(MyClass, OnConnect, error)
{
    if (error == SFERR_NO_ERROR) {

        // get Content-Type field of HTTP request header
        TRACE("content type = %s", _http.GetType().GetCString());
        ...
    }
}

SFXHTTPConnection::GetUserAgent
Get the user agent.
[ public, const ]
SFXAnsiStringConstRef GetUserAgent(Void);

Reference

SFXHTTPConnection::SetUserAgent


SFXHTTPConnection::Open
Open the HTTP/HTTPS connection.
[ public ]
SFCError Open(
    AEECLSID id = AEECLSID_WEB   // ClassID
);

Return value

  • Success : SFERR_NO_ERROR
  • If connection has already been established : SFERR_INVALID_STATE
  • If insufficient memory : SFERR_NO_MEMORY
  • If failed : SFERR_FAILED

Description

Initialize the HTTP connection. Internally generate the instance of SFBWeb class.

Example

// define _http, SFXHTTPConnection class instance, as member variable of class
class MyClass {
private:
    SFXHTTPConnection _http;
public:
    Void Start(Void);
};

// start HTTP connection
Void MyClass::Start(Void)
{
    SFCError error(SFERR_NO_ERROR);

    // open HTTP connection
    if ((error = _http.Open()) == SFERR_NO_ERROR) {
        ...
    }

    if (error != SFERR_NO_ERROR) {
        // if error occurs
        ...
    }
    return;
}

Reference

SFXHTTPConnection::Connect


SFXHTTPConnection::SetMethod
Set the HTTP request method.
[ public ]
SFCError SetMethod(
    SFXAnsiStringConstRef param   // method (character string)
);

Return value

  • Success : SFERR_NO_ERROR
  • If connection has not been established : SFERR_INVALID_STATE
  • If insufficient memory : SFERR_NO_MEMORY

Description

The value set by the SFXHTTPConnection::SetMethod function is effective until the SFXHTTPConnection::Close function is called.

The methods supported by the BREW IWeb interface are available in the SFXHTTPConnection::SetMethod function: "GET", "POST", "HEAD" etc. can be used.

If setting of the HTTP request method is obmitted, the "GET" method is used by default.

Example

// define _http, instance of SFXHTTPConnection class, as member variable
class MyClass {
private:
    SFXHTTPConnection _http;
public:
    Void Start(Void);
};

// start HTTP connection
Void MyClass::Start(Void)
{
    SFCError error(SFERR_NO_ERROR);

    // open HTTP connection
    if ((error = _http.Open()) == SFERR_NO_ERROR) {

        // set HTTP request method to "GET"
        _http.SetMethod("GET");
        ...
    }

    if (error != SFERR_NO_ERROR) {
        // if error occurs
        ...
    }
    return;
}

Reference

SFXHTTPConnection::GetMethod


SFXHTTPConnection::SetProxyServer
Set the proxy server.
[ public ]
SFCError SetProxyServer(
    SFXSocketAddressConstRef param   // proxy server
);

Return value

  • Success : SFERR_NO_ERROR
  • If connection has not been established : SFERR_INVALID_STATE
  • If insufficient memory : SFERR_NO_MEMORY

Description

Set the proxy server of SFXSocketAddress class.

Example

// define _http, instance of SFXHTTPConnection class, as member variable
class MyClass {
private:
    SFXHTTPConnection _http;
public:
    Void Start(Void); 
};

// start HTTP connection
Void MyClass::Start(Void)
{
    SFCError error(SFERR_NO_ERROR);

    // open HTTP connection
    if ((error = _http.Open()) == SFERR_NO_ERROR) {

        // set proxy server
        error = _http.SetProxyServer(SFXSocketAddress("exampleproxy.co.jp:8080"));;
        ...
    }

    if (error != SFERR_NO_ERROR) {  
        // if error occurs
        ...
    }
    return;
}

Reference

SFXHTTPConnection::GetProxyServer


SFXHTTPConnection::SetRequestContent
Set the instance of SFBSource class that manages data to be sent.
[ public ]
SFCError SetRequestContent(
    SFXStorageConstRef param   // storage of the data to send
);
[ public ]
SFCError SetRequestContent(
    SFBSourceSmpConstRef param   // SFBSource class instance of the data to send
);

Return value

  • Success : SFERR_NO_ERROR
  • If argument is null : SFERR_INVALID_PARAM
  • If connection has not been established : SFERR_INVALID_STATE
  • If insufficient memory : SFERR_NO_MEMORY

Description

[Caution] Caution

When using the SFXHTTPConnection::SetRequestContent function, the SFXHTTPConnection::GetStreamWriter function and SFXHTTPConnection::GetStreamReader function cannot be used.

Reference

SFXHTTPConnection::GetRequestContent


SFXHTTPConnection::SetRequestFlag
Set the HTTP request flag.
[ public ]
SFCError SetRequestFlag(
    UInt32 param   // flag
);

Return value

  • Success : SFERR_NO_ERROR
  • If connection has not been established : SFERR_INVALID_STATE

Reference

SFXHTTPConnection::GetRequestFlag


SFXHTTPConnection::SetRequestHeader
Set the HTTP request header.
[ public ]
SFCError SetRequestHeader(
    SFXAnsiStringConstRef key     // header name
    SFXAnsiStringConstRef value   // header value
);

Return value

  • Success : SFERR_NO_ERROR
  • If header name is User-Agent, or Content-Length, or Connection:SFERR_INVALID_PARAM
  • If connection has not been established : SFERR_INVALID_STATE
  • If insufficient memory : SFERR_NO_MEMORY

Description

If the SFXHTTPConnection::SetRequestHeader function is called several times with the same key, all the requests are stored together and then will be sent at a time.

The values set by the SFXHTTPConnection::SetRequestHeader function will be destroyed when the SFXHTTPConnection::Close function is called or the connection to the Web server is established.

Reference

SFXHTTPConnection::GetRequestHeader


SFXHTTPConnection::SetTrustMode
Set the SSL trust mode.
[ public ]
SFCError SetTrustMode(
    UInt32 param   // SSL trust mode
);

Return value

  • Success : SFERR_NO_ERROR
  • If connection has not been established : SFERR_INVALID_STATE

Description

The value set by the SFXHTTPConnection::SetTrustMode function is valid until the SFXHTTPConnection::Close function is called.

The SSL_RESULT_SERV_VERS error might occur on the real device when calling the SFXHTTPConnection::Connect function immediately after this value is changed.

To avoid this error, call the SFXHTTPConnection::Close function once, set the SSL trust mode, and then call the SFXHTTPConnection::Connect function.

One of the following values are available for the SSL trust mode:

  • SSL_TRUST_MODE_FAIL: Default, fail on trust errors.
  • SSL_TRUST_MODE_CHECK: Suspend on trust errors so they can be checked and ignored.
  • SSL_TRUST_MODE_IGNORE: Ignore all trust errors.
  • SSL_TRUST_MODE_ALWAYS: Always suspend so trust can always be checked.
[Note] Note
Detailed information: ISSL_NegotiateV in the BREW API Reference

Example

// define _http, instance of SFXHTTPConnection class, as member variable
class MyClass {
private:
    SFXHTTPConnection _http;
public:
    Void Start(Void);   
    CALLBACK_DECLARE_SFXHTTPCONNECTION(OnConnect) 
};

// start HTTP connection
Void MyClass::Start(Void)
{
    SFCError error(SFERR_NO_ERROR);

    // open HTTP connection
    if ((error = _http.Open()) == SFERR_NO_ERROR) {
    
        // set SSL trust mode
        _http.SetTrustMode(SSL_TRUST_MODE_FAIL);
        
        // connect to Web server
        if ((error = _http.Connect("http://www.example.com/",
                         CALLBACK_FUNCTION(OnConnect))) == SFERR_NO_ERROR) {
           ...
        }else{
            // if error occurs
            _http.Close();
        }
        
    }

    if (error != SFERR_NO_ERROR) {  
        // if error occurs
        ...
    }
    return;
}
// callback function notified of connection establishment
CALLBACK_IMPLEMENT_SFXHTTPCONNECTION(MyClass, OnConnect, error)
{
    if (error == SFERR_NO_ERROR) {

        ...

    }
}

Reference

BREW API ISSL_NegotiateV | SFXHTTPConnection::GetTrustMode | SFXHTTPConnection::Connect | SFXHTTPConnection::Close


SFXHTTPConnection::SetUserAgent
Set the user agent.
[ public ]
SFCError SetUserAgent(
    SFXAnsiStringConstRef param   // user agent
);

Return value

  • Success : SFERR_NO_ERROR
  • If connection has not been established : SFERR_INVALID_STATE
  • If insufficient memory : SFERR_NO_MEMORY

Reference

SFXHTTPConnection::GetUserAgent


SFXHTTPConnection::CallbackSPP
Prototype of the callback function.
typedef Void(* SFXHTTPConnection::CallbackSPP)(SFCError error, VoidPtr reference)

Description

SFXHTTPConnection::CallbackSPP is the type of callback function for the SFXHTTPConnection class.

The result of the HTTP/HTTPS communication is notified to this callback function.

The 1st argument is an error code, and the 2nd argument is a parameter for the callback function(in general, instance of SFXHTTPConnection class).

Reference

SFXHTTPConnection::Connect