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

This class defines a simple yet flexible interface which allows an application to construct an entity (a sequence of bytes) by specifying the name and value for one or more form fields. When all the desired fields have been added, the entity may be extracted and submitted to a Web server via a HTTP POST request. The Emulator part of the name alludes to the fact that there is no actual form (or any HTML) involved in the process, but the entity that is sent to the server is constructed just as it would be if a HTML form was sent from a Web browser.
The HTML 4.0 Recommendation defines two encoding methods which can be used for transporting form data: application/x-www-form-urlencoded and multipart/form-data. OpenTop provides implementations for both of these encodings via the URLEncodedFormEmulator and MultipartFormEmulator classes.
using namespace ot;
using namespace ot::web;
void uploadFile(const URL& url, const File& file)
{
// create a form emulator for multipart/form-data
RefPtr<FormEmulator> rpForm(new MultipartForEmulator);
// populate the form...
rpForm->addFilenameField(OT_T("theFile"), file);
// construct an HTTP request from the form data...
HttpRequest request(url);
request.setEntity(HttpRequest::InputStreamEntity,
rpForm->getEntityLength(), 0,
rpForm->getEntity(), rpForm->getContentTypeHeader());
// and finally send the request complete with form entity
HttpClient http;
HttpResponse response = http.sendRequest(request);
if(response.getResponseCode() != 200)
{
throw IOException(response.getResponseLine());
}
}
| Method Summary | |
virtual void |
addField(const String& fieldName, const String& value)=0Inserts 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)=0Inserts 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)=0Inserts a filename field to the end of the sequence of fields contained by this form. |
virtual void |
clear()=0Clears all the fields, returning the form to its newly constructed state. |
virtual String |
getContentTypeHeader() const=0Returns a Content-Type header value describing the MIME type of the form entity. |
virtual RefPtr< InputStream > |
getEntity() const=0Returns an InputStream which provides a contiguous stream of bytes constituting the form entity. |
virtual Int64Type |
getEntityLength() const=0Returns the length of the form entity. |
virtual void |
writeEntity(OutputStream* pStream) const=0Writes the form entity to the supplied OutputStream. |
| Methods inherited from class ot::ManagedObject |
addRef(), getRefCount(), onFinalRelease(), operator=(const ManagedObject&), release() |
| Method Detail |
virtual void addField(const String& fieldName,
const String& value)=0
fieldName - value - virtual void addFilenameField(const String& fieldName,
const File& file,
const String& filename,
const String& encoding,
const String& mimeType)=0
fieldName - file - filename - encoding - mimeType - UnsupportedOperationException - virtual void addFilenameField(const String& fieldName,
InputStream* pStream,
const String& filename,
const String& encoding,
Int64Type fileLength,
const String& mimeType)=0
fieldName - pStream - filename - encoding - mimeType - UnsupportedOperationException - IllegalArgumentException - virtual void clear()=0
virtual String getContentTypeHeader() const=0
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=0
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 - virtual Int64Type getEntityLength() const=0
virtual void writeEntity(OutputStream* pStream) const=0
pStream - IOException - NullPointerException -
|
OpenTop 1.5 | |||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | Cross-Platform C++ | ||||||
| SUMMARY: CONSTRUCTOR | METHOD | DETAIL: CONSTRUCTOR | METHOD | |||||||