QHttpMultiPart Class

The QHttpMultiPart class resembles a MIME multipart message to be sent over HTTP. 更多...

头文件: #include <QHttpMultiPart>
qmake: QT += network
开始支持版本: Qt 4.8
基类: QObject

公有类型

enum ContentType { MixedType, RelatedType, FormDataType, AlternativeType }

公有函数

QHttpMultiPart(QObject *parent = Q_NULLPTR)
QHttpMultiPart(ContentType contentType, QObject *parent = Q_NULLPTR)
~QHttpMultiPart()
void append(const QHttpPart &httpPart)
QByteArray boundary() const
void setBoundary(const QByteArray &boundary)
void setContentType(ContentType contentType)
  • 32 个公有函数继承自 QObject

其他继承的成员

  • 1 个属性继承自 QObject
  • 1 个公有槽函数继承自 QObject
  • 2 个信号继承自 QObject
  • 1 个公有变量继承自 QObject
  • 10 个静态公有成员继承自 QObject
  • 9 个受保护的函数继承自 QObject
  • 2 个受保护的变量继承自 QObject

详细描述

The QHttpMultiPart class resembles a MIME multipart message to be sent over HTTP.

The QHttpMultiPart resembles a MIME multipart message, as described in RFC 2046, which is to be sent over HTTP. A multipart message consists of an arbitrary number of body parts (see QHttpPart), which are separated by a unique boundary. The boundary of the QHttpMultiPart is constructed with the string "boundary_.oOo._" followed by random characters, and provides enough uniqueness to make sure it does not occur inside the parts itself. If desired, the boundary can still be set via setBoundary().

As an example, consider the following code snippet, which constructs a multipart message containing a text part followed by an image part:


  QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);

  QHttpPart textPart;
  textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"text\""));
  textPart.setBody("my text");

  QHttpPart imagePart;
  imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
  imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"image\""));
  QFile *file = new QFile("image.jpg");
  file->open(QIODevice::ReadOnly);
  imagePart.setBodyDevice(file);
  file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart

  multiPart->append(textPart);
  multiPart->append(imagePart);

  QUrl url("http://my.server.tld");
  QNetworkRequest request(url);

  QNetworkAccessManager manager;
  QNetworkReply *reply = manager.post(request, multiPart);
  multiPart->setParent(reply); // delete the multiPart with the reply
  // here connect signals etc.

参见 QHttpPart and QNetworkAccessManager::post().

成员类型

enum QHttpMultiPart::ContentType

List of known content types for a multipart subtype as described in RFC 2046 and others.

ConstantValueDescription
QHttpMultiPart::MixedType0corresponds to the "multipart/mixed" subtype, meaning the body parts are independent of each other, as described in RFC 2046.
QHttpMultiPart::RelatedType1corresponds to the "multipart/related" subtype, meaning the body parts are related to each other, as described in RFC 2387.
QHttpMultiPart::FormDataType2corresponds to the "multipart/form-data" subtype, meaning the body parts contain form elements, as described in RFC 2388.
QHttpMultiPart::AlternativeType3corresponds to the "multipart/alternative" subtype, meaning the body parts are alternative representations of the same information, as described in RFC 2046.

参见 setContentType().

成员函数

QHttpMultiPart::QHttpMultiPart(QObject *parent = Q_NULLPTR)

Constructs a QHttpMultiPart with content type MixedType and sets parent as the parent object.

参见 QHttpMultiPart::ContentType.

QHttpMultiPart::QHttpMultiPart(ContentType contentType, QObject *parent = Q_NULLPTR)

Constructs a QHttpMultiPart with content type contentType and sets parent as the parent object.

参见 QHttpMultiPart::ContentType.

QHttpMultiPart::~QHttpMultiPart()

Destroys the multipart.

void QHttpMultiPart::append(const QHttpPart &httpPart)

Appends httpPart to this multipart.

QByteArray QHttpMultiPart::boundary() const

returns the boundary.

参见 setBoundary().

void QHttpMultiPart::setBoundary(const QByteArray &boundary)

Sets the boundary to boundary.

Usually, you do not need to generate a boundary yourself; upon construction the boundary is initiated with the string "boundary_.oOo._" followed by random characters, and provides enough uniqueness to make sure it does not occur inside the parts itself.

参见 boundary().

void QHttpMultiPart::setContentType(ContentType contentType)

Sets the content type to contentType. The content type will be used in the HTTP header section when sending the multipart message via QNetworkAccessManager::post(). In case you want to use a multipart subtype not contained in QHttpMultiPart::ContentType, you can add the "Content-Type" header field to the QNetworkRequest by hand, and then use this request together with the multipart message for posting.

参见 QHttpMultiPart::ContentType and QNetworkAccessManager::post().