How to Send a Multipart Message to a XHTML Browser

As we know that wireless networks have longer latencies and less bandwidth than wireline networks, we may take a longer time to navigate relevant pages in handsets. To optimize the use of wireless networks and make applications more responsive to the user, it is useful to send out a multipart message to the end user so that the user can navigate among contents that are stored in the device and avoid sending requests to the server for each part. In this tech note, we will introduce how to send a multipart/mixed message to the XHTML browser.

Multipart/mixed content type is intended for use when the body parts are independent and intended to be displayed serially. For more information, please refer to http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html.

Let's take a close look at the following Perl code that generates a multipart/mixed message. You can also use your favorite language like ASP to implement it.

#!/usr/bin/perl
print "Content-type: multipart/mixed; boundary=foobarbam","\n\n";
The above line defines a multipart Content-type header field. The parameter "boundary" is used to specify the encapsulation boundary. This indicates the entity consists of several parts and each part should begin with the "boundary" parameter.
print "--foobarbam", "\n";
print "Content-type: text/html", "\n";
print "Content-Location: page1 \n\n";
The first line defines the encapsulation boundary as a line that begins with two hyphen characters followed by the "boundary" parameter.

The second line defines the content-type for this part and the third line sets the content-location of this part as page1. Please note that two break lines following Content-Location are required.
print "<html>", "\n";
print "<body>", "\n";
print "<p>", "\n";
print "Page 1<br/>", "\n";
print '<a href="page2">Page 2</a>', "\n"; 
print "</p> \n";
print "</body> \n";
print "</html>","\n";
These lines output a HTML content as page 1 in which a link jumping to page 2 is embedded.
print "--foobarbam", "\n";
print "Content-Type: text/html", "\n";
print "Content-Location: page2", "\n\n";
The first line prints out the boundary line again to indicate the beginning of another part. Other two lines define the content-type and content-location.
print "<html>", "\n";
print "<body>", "\n";
print  "<p> \n";
print "Page2!", "\n";
print  '<a href="page1">Page 1</a>'; 
print "</p>", "\n";
print "</body>", "\n";
print "</html>", "\n";
These lines output a HTML content as page 2 that has a link to go back to page 1.
print "--foobarbam--", "\n";
exit (0);
The first line indicates the end of multipart entity. Two hyphens following the "boundary" parameter is a required format.

After this code is run, the HTTP response is as follows:

HTTP/1.1 200 OK Date: Thu, 16 May 2002 22:40:42 GMT Server: Apache/1.3.19 (Unix) Connection: close Content-Type: multipart/mixed; boundary=foobarbam

--foobarbam Content-type: text/html Content-Location: page1
<html>
<body>
<p>
Page 1<br/>
<a href="page2">Page 2</a>
</p> 
</body> 
</html>
--foobarbam
Content-Type: text/html
Content-Location: page2
<html>
<body>
<p> 
Page2!
<a href="page1">Page 1</a></p>
</body>
</html>
--foobarbam--
This application has been tested on SDK 5.1. Please try the demo URL at http://demo.phone.com/test/multipart.cgi