QHttpPart Class

The QHttpPart class holds a body part to be used inside a HTTP multipart MIME message. 更多...

头: #include <QHttpPart>
qmake: QT += network
Since: Qt 4.8

公共函数

QHttpPart ()
QHttpPart (const QHttpPart & other )
~QHttpPart ()
void setBody (const QByteArray & body )
void setBodyDevice (QIODevice * device )
void setHeader (QNetworkRequest::KnownHeaders header , const QVariant & value )
void setRawHeader (const QByteArray & headerName , const QByteArray & headerValue )
void swap (QHttpPart & other )
bool operator!= (const QHttpPart & other ) const
QHttpPart & operator= (QHttpPart && other )
QHttpPart & operator= (const QHttpPart & other )
bool operator== (const QHttpPart & other ) const

详细描述

The QHttpPart class holds a body part to be used inside a HTTP multipart MIME message.

The QHttpPart class holds a body part to be used inside a HTTP multipart MIME message (which is represented by the QHttpMultiPart class). A QHttpPart consists of a header block and a data block, which are separated by each other by two consecutive new lines. An example for one part would be:

Content-Type: text/plain
Content-Disposition: form-data; name="text"
here goes the body
					

For setting headers, use setHeader () 和 setRawHeader (), which behave exactly like QNetworkRequest::setHeader () 和 QNetworkRequest::setRawHeader ().

For reading small pieces of data, use setBody (); for larger data blocks like e.g. images, use setBodyDevice (). The latter method saves memory by not copying the data internally, but reading directly from the device. This means that the device must be opened and readable at the moment when the multipart message containing the body part is sent on the network via QNetworkAccessManager::post ().

为构建 QHttpPart with a small body, consider the following snippet (this produces the data shown in the example above):

QHttpPart textPart;
textPart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/plain"));
textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"text\""));
textPart.setBody("here goes the body");
					

为构建 QHttpPart reading from a device (e.g. a file), the following can be applied:

QHttpPart imagePart;
imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"image\""));
imagePart.setRawHeader("Content-ID", "my@content.id"); // add any headers you like via setRawHeader()
QFile *file = new QFile("image.jpg");
file->open(QIODevice::ReadOnly);
imagePart.setBodyDevice(file);
					

注意, QHttpPart does not take ownership of the device when set, so it is the developer's responsibility to destroy it when it is not needed anymore. A good idea might be to set the multipart message as parent object for the device, as documented at the documentation for QHttpMultiPart .

另请参阅 QHttpMultiPart and QNetworkAccessManager .

成员函数文档编制

QHttpPart:: QHttpPart ()

构造空的 QHttpPart 对象。

QHttpPart:: QHttpPart (const QHttpPart & other )

创建副本为 other .

QHttpPart:: ~QHttpPart ()

销毁此 QHttpPart .

void QHttpPart:: setBody (const QByteArray & body )

Sets the body of this MIME part to body . The body set with this method will be used unless the device is set via setBodyDevice (). For a large amount of data (e.g. an image), use setBodyDevice (), which will not copy the data internally.

另请参阅 setBodyDevice ().

void QHttpPart:: setBodyDevice ( QIODevice * device )

Sets the device to read the content from to device . For large amounts of data this method should be preferred over setBody (), because the content is not copied when using this method, but read directly from the device. device must be open and readable. QHttpPart 未拥有所有权对于 device , i.e. the device must be closed and destroyed if necessary. if device is sequential (e.g. sockets, but not files), QNetworkAccessManager::post () should be called after device has emitted finished(). For unsetting the device and using data set via setBody (), use "setBodyDevice(0)".

另请参阅 setBody () 和 QNetworkAccessManager::post ().

void QHttpPart:: setHeader ( QNetworkRequest::KnownHeaders header , const QVariant & value )

Sets the value of the known header header value , overriding any previously set headers.

另请参阅 QNetworkRequest::KnownHeaders , setRawHeader (),和 QNetworkRequest::setHeader ().

void QHttpPart:: setRawHeader (const QByteArray & headerName , const QByteArray & headerValue )

设置 Header (头) headerName 到值 headerValue 。若 headerName 对应已知 Header (头部) (见 QNetworkRequest::KnownHeaders ),原生格式将被剖析,相应 cooked 头也将被设置。

注意: 设置相同的头 2 次覆盖先前设置。要完成相同名称多个 HTTP 头的行为,应采用逗号 , 分隔它们并串联这 2 个值,然后设置一个原生 Header (头)。

另请参阅 QNetworkRequest::KnownHeaders , setHeader (),和 QNetworkRequest::setRawHeader ().

void QHttpPart:: swap ( QHttpPart & other )

Swaps this HTTP part with other 。此函数非常快且从不失败。

该函数在 Qt 5.0 引入。

bool QHttpPart:: operator!= (const QHttpPart & other ) const

返回 true if this object is not the same as other .

另请参阅 operator== ().

QHttpPart &QHttpPart:: operator= ( QHttpPart && other )

移动赋值运算符。

QHttpPart &QHttpPart:: operator= (const QHttpPart & other )

创建副本为 other .

bool QHttpPart:: operator== (const QHttpPart & other ) const

返回 true if this object is the same as other (i.e., if they have the same headers and body).

另请参阅 operator!= ().