The QHttpPart class holds a body part to be used inside a HTTP multipart MIME message. More...
Header: #include <QHttpPart>
qmake: QT += network
Since: Qt 4.8
QHttpPart类包含一个要在HTTP多部分MIME消息中使用的主体部分。更多
需要添加QHttpPart头文件
qmake + network
所有成员的列表,包括继承的成员
Public Functions
public函数
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=(const QHttpPart & other)
bool operator==(const QHttpPart & other) const
Detailed Description 详细说明
The QHttpPart class holds a body part to be used inside a HTTP multipart MIME message.
QHttpPart类包含一个要在HTTP多部分MIME消息中使用的主体部分。
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:
QHttpPart类保存要在HTTP多部分MIME消息(由QHttpMultiPart类表示)中使用的主体部分。QHttpPart由一个头块和一个数据块组成,这两个块由两个连续的新行隔开。一个零件的例子是:
Content-Type: text/plain
Content-Disposition: form-data; name="text"
here goes the body
For setting headers, use setHeader() and setRawHeader(), which behave exactly like QNetworkRequest::setHeader() and QNetworkRequest::setRawHeader().
对于设置标头,请使用setHeader()和setRawHeader(),它们的行为与QNetworkRequest::setRawHeader()和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().
要读取小块数据,请使用setBody();对于较大的数据块(如图像),请使用setBodyDevice()。而不是直接从存储器中拷贝数据。这意味着,当包含主体部分的多部分消息通过QNetworkAccessManager::post()在网络上发送时,设备必须打开并可读。
To construct a QHttpPart with a small body, consider the following snippet (this produces the data shown in the example above):
要构造一个具有小主体的QHttpPart,请考虑以下代码片段(这将生成上面示例中显示的数据):
QHttpPart textPart;
textPart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/plain"));
textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name="text""));
textPart.setBody("here goes the body");
To construct a QHttpPart reading from a device (e.g. a file), the following can be applied:
要构建从设备(例如文件)读取的QHttpPart,可以使用以下方法:
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);
Be aware that 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.
请注意,QHttpPart在设置时并不拥有该设备的所有权,因此开发人员有责任在不再需要时销毁它。一个好主意是将multipart消息设置为设备的父对象,如QHttpMultiPart文档中所述。
See also QHttpMultiPart and QNetworkAccessManager.另请参见QHttpMultiPart和QNetworkAccessManager。
Member Function Documentation
QHttpPart::QHttpPart()
Constructs an empty QHttpPart object.
QHttpPart::QHttpPart(const QHttpPart & other)
Creates a copy of other.
QHttpPart::~QHttpPart()
Destroys this 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.
将此MIME部分的正文设置为正文。除非通过setBodyDevice()设置设备,否则将使用此方法设置的正文。对于大量数据(如图像),请使用setBodyDevice(),它不会在内部复制数据。
See also 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 does not take ownership of 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(),因为使用此方法时不会复制内容,而是直接从设备读取。设备必须打开且可读。QHttpPart不拥有设备的所有权,也就是说,必要时必须关闭并销毁设备。如果设备是顺序的(例如,套接字,而不是文件),则应在设备发出finished()之后调用QNetworkAccessManager::post()。要取消设置设备并通过setBody()使用数据集,请使用“setBodyDevice(0)”。
See also setBody() and QNetworkAccessManager::post().
void QHttpPart::setHeader(QNetworkRequest::KnownHeaders header, const QVariant & value)
Sets the value of the known header header to be value, overriding any previously set headers.
将已知标头标头的值设置为value,覆盖以前设置的任何标头。
See also QNetworkRequest::KnownHeaders, setRawHeader(), and QNetworkRequest::setHeader().
void QHttpPart::setRawHeader(const QByteArray & headerName, const QByteArray & headerValue)
Sets the header headerName to be of value headerValue. If headerName corresponds to a known header (see QNetworkRequest::KnownHeaders), the raw format will be parsed and the corresponding "cooked" header will be set as well.将头头名称设置为值headerValue。
如果headerName对应于已知的标头(请参阅QNetworkRequest::KnownHeader),则将解析原始格式,并设置相应的“熟”标头。
Note: setting the same header twice overrides the previous setting. To accomplish the behaviour of multiple HTTP headers of the same name, you should concatenate the two values, separating them with a comma (",") and set one single raw header.
注意:设置相同的标题两次会覆盖先前的设置。要完成相同名称的多个HTTP报头的行为,应该将这两个值连接起来,用逗号(“,”)分隔它们,并设置一个单独的raw头。
See also QNetworkRequest::KnownHeaders, setHeader(), and QNetworkRequest::setRawHeader().
void QHttpPart::swap(QHttpPart & other)
Swaps this HTTP part with other. This function is very fast and never fails.
将此HTTP部分与其他部分交换。这个功能非常快,从不失败。
This function was introduced in Qt 5.0.
bool QHttpPart::operator!=(const QHttpPart & other) const
Returns true if this object is not the same as other.
如果此对象与其他对象不同,则返回true。
See also operator==().
QHttpPart & QHttpPart::operator=(const QHttpPart & other)
Creates a copy of other.
bool QHttpPart::operator==(const QHttpPart & other) const
Returns true if this object is the same as other (i.e., if they have the same headers and body).
See also operator!=().