|
OpenTop 1.5 | |||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | Cross-Platform C++ | ||||||
| SUMMARY: CONSTRUCTOR | METHOD | DETAIL: CONSTRUCTOR | METHOD | |||||||
#include "ot/web/URLEncodedFormEmulator.h"

application/x-www-form-urlencoded is defined in the HTML 4.0 Recommendation, but even though HTML is designed to be transmitted over an 8-bit protocol (HTTP), URLs are designed to be sent over protocols which only permit 7-bit byte values. They are also designed to be parsed quite simply, with the first whitespace character indicating the end of a URL. To accommodate these two requirements, certain control characters and non-ascii characters are encoded using an escape sequence.
Specifically, Unicode strings are application/x-www-form-urlencoded by first converting each Unicode character into a sequence of bytes using a specified transport encoding (the default is UTF-8). The resulting byte sequence is then transformed once more to replace non-ascii byte values with a %HH escape mechanism (where HH is the hexadecimal value of the byte it replaces). The equal sign and ampersand have special meaning so they are escaped when they appear in field names and values. As whitespace is not permitted within the entity, the space character is replaced with a '+'.
using namespace ot;
using namespace ot::web;
RefPtr<InputStream> searchGoogle(const String& keywords)
{
// create a form emulator for application/x-www-form-urlencoded
// to help construct the request URL
RefPtr<FormEmulator> rpForm(new URLEncodedForEmulator);
// populate the form...
rpForm->addField(OT_T("q"), keywords);
// ask Google to return a French results page
rpForm->addField(OT_T("hl"), OT_T("fr"));
// create a URL to perform the Google search
String google = OT_T("http://www.google.com/search?");
URL googleSearchURL(google + rpForm->getEntityAsString());
// perform a HTTP GET request...
HttpRequest request(googleSearchURL);
HttpClient http;
HttpResponse response = http.sendRequest(request);
// ... and return the server's response
if(response.getResponseCode() == 200)
return response.getInputStream();
else
throw IOException(response.getResponseLine());
}
| Constructor/Destructor Summary | |
URLEncodedFormEmulator()Default constructor. | |
URLEncodedFormEmulator(const String& encoding)Constructs a URLEncodedFormEmulator with a specified Unicode encoding name. | |
| Method Summary | |
virtual void |
addField(const String& name, const String& value)Inserts a name/value pair field to the end of the sequence of fields contained by this form. |
virtual void |
addFilenameField(const String& fieldName, const File& file, const String& filename, const String& encoding, const String& mimeType)Inserts a filename field to the end of the sequence of fields contained by this form. |
virtual void |
addFilenameField(const String& fieldName, InputStream* pStream, const String& filename, const String& encoding, Int64Type fileLength, const String& mimeType)Inserts a filename field to the end of the sequence of fields contained by this form. |
virtual void |
clear()Clears all the fields, returning the form to its newly constructed state. |
virtual String |
getContentTypeHeader() constReturns a Content-Type header value describing the MIME type of the form entity. |
virtual RefPtr< InputStream > |
getEntity() constReturns an InputStream which provides a contiguous stream of bytes constituting the form entity. |
String |
getEntityAsString() constReturns the entity for this form as a String. |
virtual Int64Type |
getEntityLength() constReturns the length of the form entity. |
void |
writeEntity(OutputStream* pOutputStream) constWrites the form entity to the supplied OutputStream. |
| Methods inherited from class ot::ManagedObject |
addRef(), getRefCount(), onFinalRelease(), operator=(const ManagedObject&), release() |
| Constructor/Destructor Detail |
URLEncodedFormEmulator()
URLEncodedFormEmulator(const String& encoding)
UnsupportedEncodingException - | Method Detail |
virtual void addField(const String& name,
const String& value)
fieldName - value - virtual void addFilenameField(const String& fieldName,
const File& file,
const String& filename,
const String& encoding,
const String& mimeType)
UnsupportedOperationException - virtual void addFilenameField(const String& fieldName,
InputStream* pStream,
const String& filename,
const String& encoding,
Int64Type fileLength,
const String& mimeType)
fieldName - pStream - filename - encoding - mimeType - UnsupportedOperationException - IllegalArgumentException - virtual void clear()
virtual String getContentTypeHeader() const
It is advisable to use this method when constructing a HTTP request rather than assuming a constant value for the Content-Type header. One important reason for this is that the Content-Type header contains a variable separator parameter when describing the multipart/form-data encoding.
virtual RefPtr< InputStream > getEntity() const
This function can be called repeatedly, with each invocation returning a RefPtr to a new InputStream. In most cases the returned InputStream can be read without affecting the internal state of this form because it is generated from the form's field without altering their state. The notable exception to this case is when a filename field has been added using the overloaded method which takes an InputStream parameter. In this case the filename field's InputStream is returned as part of a SequenceInputStream, and this stream can be read only once. In this case, subsequent calls to this function will result in an IOException.
IOException - String getEntityAsString() const
virtual Int64Type getEntityLength() const
void writeEntity(OutputStream* pOutputStream) const
pStream - IOException - NullPointerException -
|
OpenTop 1.5 | |||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | Cross-Platform C++ | ||||||
| SUMMARY: CONSTRUCTOR | METHOD | DETAIL: CONSTRUCTOR | METHOD | |||||||