Task: HTTP Client

This tutorial, based on the HttpClient sample delivered with the bada SDK in \<BADA_SDK_HOME>\Samples\HttpClient, demonstrates how you can use the Osp::Net::Http sub-namespace to handle HTTP transactions in bada. For more information on the sample functionality, see HttpClient Sample Overview.

This tutorial builds upon elements described in Making HTTP Requests and Receiving HTTP Responses, and consists of the following parts:

The tutorial does not cover information on general bada C++ application structure or on using the UI elements; for those issues, see App Tutorial and UI Tutorial.

This sample is a fully functional application which can establish an HTTP session in which multiple transactions can occur between the bada device and the host URI. The transactions include sending a request to the host URI and receiving a response for the request.

The application includes the following UI controls:

Figure: HttpClient form

HttpClient form

The HttpClient sample application uses the bada Form Based Application template, which contains a preset Osp::Ui::Controls::Form class. Most methods used in the sample remain as they were created in the project template.

To Initialize the Application

  1. HttpClient.h Header File
    1. HTTP services can be implemented in bada by including the FNet.h file in the application.

      #include <FApp.h>
      #include <FUi.h>
      #include <FNet.h>
      #include <FSystem.h>
    2. The Osp::Net::Http::IHttpTransactionEventListener interface is used for listening for and responding to HTTP transaction events.

      class HttpClient: 
         public Osp::App::Application,
         public Osp::System::IScreenEventListener,
         public Osp::Ui::IActionEventListener,
         public Osp::Net::Http::IHttpTransactionEventListener
      {
    3. The event handlers related to the listener interface are also declared.

         public:
            // Basic event handlers are declared
      	
            // IHttpTransactionEventListener
            void OnTransactionReadyToRead(Osp::Net::Http::HttpSession& httpSession,
                                          Osp::Net::Http::HttpTransaction& httpTransaction,
                                          int availableBodyLen);
      
            void OnTransactionAborted(Osp::Net::Http::HttpSession& httpSession,
                                      Osp::Net::Http::HttpTransaction& httpTransaction, result r);
      
            void OnTransactionReadyToWrite(Osp::Net::Http::HttpSession& httpSession,
                                           Osp::Net::Http::HttpTransaction& httpTransaction,
                                           int recommendedChunkSize);
      
            void OnTransactionHeaderCompleted(Osp::Net::Http::HttpSession& httpSession,
                                              Osp::Net::Http::HttpTransaction& httpTransaction,
                                              int headerLen, bool rs);
      
            void OnTransactionCompleted(Osp::Net::Http::HttpSession& httpSession,
                                        Osp::Net::Http::HttpTransaction& httpTransaction);
      
            void OnTransactionCertVerificationRequiredN(Osp::Net::Http::HttpSession& httpSession, 
                                                        Osp::Net::Http::HttpTransaction& httpTransaction,
                                                        Osp::Base::String* pCert);
    4. Finally, a user-defined method is declared along with the required variables and pointers for the application.

         private:
            result TestHttpGet();
      
         private:
            Osp::Net::Http::HttpSession* __pSession;
            // Pointers to the required Ui controls
      };

To Send an HTTP Request

  1. HttpClient.cpp Source File
    1. An Osp::Net::Http::HttpSession class instance is created. This class creates a session within which a set of HTTP transactions can take place. The Construct() method is used to open an HTTP session in the normal mode.

      result HttpClient::TestHttpGet()
      {
         result r = E_SUCCESS;
         HttpTransaction* pTransaction = null;
         HttpRequest* pRequest = null;
      
         String hostAddr(L"http://www.bada.com");
      
         if (__pSession == null)
         {
            __pSession = new HttpSession();
      				
            r = __pSession->Construct(NET_HTTP_SESSION_MODE_NORMAL, null, hostAddr, null);
         }
      
    2. The OpenTransactionN() method is used to open a transaction, and the AddHttpTransactionListener() method of the Osp::Net::Http::HttpTransaction class is used to add listeners to the application for the IHttpTransactionEventListener interface.

         pTransaction = __pSession->OpenTransactionN();	
      
         r = pTransaction->AddHttpTransactionListener(*this);
    3. The GetRequest() method is used to get a pointer to the Osp::Net::Http::HttpRequest class. This pointer can be used to set the URI of the request.

      The host URI is set with the SetUri() method, while the SetMethod() method is used to set the HTTP GET method, which requests the set URI for a resource.

      Finally, the Submit() method is used to send the HTTP request.

         pRequest = const_cast<HttpRequest*>(pTransaction->GetRequest());
      	
         r = pRequest->SetUri(L"www.bada.com");
      	
         r = pRequest->SetMethod(NET_HTTP_METHOD_GET);
      	
         r = pTransaction->Submit();
      
         return r;
      }
      

To Receive an HTTP Response

  1. HttpClient.cpp Source File
    1. The OnTransactionReadyToRead() event handler is used to receive the content body of an HTTP response. It receives 3 parameters: the HTTP session information, the HTTP transaction information, and the length of the HTTP transaction content body.

      The GetResponse() method is used to get the HTTP response of the current transaction.

      void HttpClient::OnTransactionReadyToRead(HttpSession& httpSession, 
                                                HttpTransaction& httpTransaction, 
                                                int availableBodyLen)
      {
         AppLog("####### OnTransactionReadyToRead! #######");
      
         HttpResponse* pHttpResponse = httpTransaction.GetResponse();
      
    2. The GetHttpStatusCode() method is used to get the HTTP status from the response header. If the status code is HTTP_STATUS_OK, the transaction is a success.

         if (pHttpResponse->GetHttpStatusCode() == HTTP_STATUS_OK)
         {
            HttpHeader* pHttpHeader = pHttpResponse->GetHeader();
      
    3. The GetRawHeaderN() method gets the HTTP header information from the response, while the ReadBodyN() method gets the content body information.

            if (pHttpHeader != null)
            {
               String* tempHeaderString = pHttpHeader->GetRawHeaderN();
      
               ByteBuffer* pBuffer = pHttpResponse->ReadBodyN();
      
               // Display the content body length in an edit area
      
               delete tempHeaderString;
               delete pBuffer;
            }
         }
      }

Where to Go Next