August 1, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Mastering Internet Programming on Mobile Devices: An Asynchronous Data Exchange

  • November 17, 2004
  • By Alex Gusev
  • Send Email »
  • More Articles »

In the previous article, we started to deal with WinInet basics. As you might see, that is not a big deal. Now, we will turn to asynchronous Internet sessions, simple HTTP authentication techniques, and using secure Internet connections. Samples will be given mostly in C++ because C# hides many API calls from the user. Nevertheless, C# programmers always can wrap it via PInvoke.

Displaying Operation Progress Status

Prior to discussing asynchronous operations over the Internet, let's touch on one important part of data transferring. Regardless of the established session type (synchronous or asynchronous), users of your application probably will appreciate it if it somehow indicates that lengthy operations are advancing and still alive. WinInet provides a callback mechanism for such purposes. An application may register such a callback to retrieve asynchronous notifications about the desired operation's progress. You should use the following function:

typedef void (CALLBACK *INTERNET_STATUS_CALLBACK) (
  HINTERNET hInternet,
  DWORD_PTR dwContext,
  DWORD dwInternetStatus,
  LPVOID lpvStatusInformation,
  DWORD dwStatusInformationLength
);

INTERNET_STATUS_CALLBACK WINAPI InternetSetStatusCallback(
  HINTERNET hInternet,
  INTERNET_STATUS_CALLBACK lpfnInternetCallback
);
InternetSetStatusCallback takes a pointer to the application-defined function, which then is called by WinInet when progress is made on the specified operation. Saying it briefly, all you need to do is to define the following function:

void CALLBACK YourInternetCallback(
  HINTERNET hInternet,
  DWORD_PTR dwContext,
  DWORD dwInternetStatus,
  LPVOID lpvStatusInformation,
  DWORD dwStatusInformationLength
);

All parameters are described in detail in online documentation, so we won't copy it here. Let's just highlight several useful points. The second parameter of the callback function is an application-defined context for the hInternet handle. This gives you a convenient way to implement different behavior for each context value; for example, for uploading and downloading data. Another thing to be noted is the dwInternetStatus parameter. You will use its values to inform the user about actual operation progress and to detect the moment when the asynchronous operation is completed. Thus, such a callback may look like the following:

void CALLBACK InternetCallback(HINTERNET hInternet,
                               DWORD_PTR dwContext,
                               DWORD dwInternetStatus,
                               LPVOID lpvStatusInformation,
                               DWORD dwStatusInformationLength)
{
    switch (dwContext)
    {
    case X:
    ...
       switch (dwInternetStatus)
       {
       ...
       case INTERNET_STATUS_CONNECTED_TO_SERVER:
            // Notify the user...
            ...
            break;
       case INTERNET_STATUS_REQUEST_COMPLETE:
            // Asynchronous request was completed
            ...
            break;
       case INTERNET_STATUS_RESPONSE_RECEIVED:
            // Process this event...
            ...
            break;
       ...
       }
       break;
    }
}

The Compact Framework still does not provide the same mechanism as C++ does, but we will see what C# offers for asynchronous calls in the next section.

Establishing Asynchronous Internet Sessions

Now, we are ready to handle asynchronous behavior. The first step is to use INTERNET_FLAG_ASYNC while calling the InternetOpen function:

hOpen = InternetOpen (L"WceHttp", INTERNET_OPEN_TYPE_PRECONFIG,
                      NULL, NULL, INTERNET_FLAG_ASYNC);

The second step is to define the callback to get notified during asynchronous operation:

INTERNET_STATUS_CALLBACK PrevStatusCallback =
     InternetSetStatusCallback(hOpen,InternetCallback);
if ( PrevStatusCallback == INTERNET_INVALID_STATUS_CALLBACK )
{
// Invalid callback was provided, handle it
...
}

I should note here that the only functions that behave asynchronously under Windows CE are InternetReadFile and InternetQueryDataAvailable. For these APIs, you can get completion status notifications. All other functions operate synchronously. Nevertheless, Windows CE supports chunked request transfers. It means that your application can send heavy requests by separate data blocks. The code shown in the next sample gives you an example of how can it be done:

// use HttpSendRequestEx to send large buffers

// set the header
if ( !HttpAddRequestHeaders(hRequest,
    lpszHttpHeader, dwHttpHeaderLength,
    HTTP_ADDREQ_FLAG_ADD) )
{
    // handle error
}

INTERNET_BUFFERS ibBuffer;
ZeroMemory (& ibBuffer, sizeof (INTERNET_BUFFERS));
ibBuffer.dwStructSize  = sizeof (INTERNET_BUFFERS);
ibBuffer.dwBufferTotal = nBufferSize;

if ( !HttpSendRequestEx (hRequest, & ibBuffer,
                         NULL, HSR_INITIATE, 0))
{
    // handle error
}

// now enter the buffer in chunks of 1024 bytes
DWORD         dwBytesSend;
int           nPortion = nBufferSize / 10;
const int     nMinToSend = __max (nPortion, g_HTTPChunkSize);
int           nChunkSize = nMinToSend;
int           nBufferPos = 0;
int           nPrevPercentage = 0;
char          *pBufferPtr = g_szGlobalBuffer;

while (nBufferPos < nBufferSize)
{
    if (! InternetWriteFile (hRequest, pBufferPtr, nChunkSize,
                             & dwBytesSend))
    {
        m_WSError.SetErrorDescr (L"InternetWriteFile", __FILE__,
                                 __LINE__, GetLastError ());
        HttpEndRequest (hRequest, NULL, 0, 0);
        goto Cleanup;
    }

    pBufferPtr += nChunkSize;
    nBufferPos += nChunkSize;
    if (nBufferSize - nBufferPos < nMinToSend)
        nChunkSize = nBufferSize - nBufferPos;
    else
        nChunkSize = nMinToSend;
}


// end the request
if ( !HttpEndRequest (hRequest, NULL, 0, 0))
{
    // handle error
}

A common procedure is something like the following:

  • Initiate request sending by a HttpSendRequestEx call with the HSR_INITIATE parameter.
  • Sequentially call InternetWriteFile to transmit all the request's data to the remote host.
  • Complete the request by a HttpEndRequest call.

In turn, on response reading, InternetReadFile in asynchronous mode returns FALSE with the error set to ERROR_IO_PENDING. Your application then receives progress notifications via the callback we discussed above.

C# makes our life easier in many standard cases; thus, the corresponding task may be implemented as shown in a simple snippet below:

  void btnSubmit_Click(Object sender, EventArgs e)
  {
    HttpWebRequest wreq = (HttpWebRequest)
                          WebRequest.Create(txtURL.Text);
    IAsyncResult r      = (IAsyncResult) wreq.BeginGetResponse(
                          new AsyncCallback(this.RespCallback),
                          wreq);
    Thread.Sleep(5000);
  }

  private void RespCallback(IAsyncResult ar)
  {
    HttpWebRequest req   = (HttpWebRequest) ar.AsyncState;
    HttpWebResponse resp = (HttpWebResponse) req.EndGetResponse(ar);

    int BytesRead = 0;
    char[] Buffer = new char[1024];

    StreamReader Reader = new StreamReader(resp.GetResponseStream(),
                                           System.Text.Encoding.UTF8);
    StringWriter Writer = new StringWriter();

    BytesRead = Reader.Read(Buffer, 0, 1024);
    while (BytesRead != 0 )
    {
      Writer.Write(Buffer, 0, 1024);
      BytesRead = Reader.Read(Buffer, 0, 1024);
    }
    txtOutput.Text = Writer.ToString();
  }

The main business here is calling the response from the server via a BeginGetResponse method. It takes a callback and user-defined 'status' object as parameters. The callback is called when the operation is completed; in other words, when the response data is ready for reading. All other things are obvious enough.





Page 1 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel