QHttpMultiPart Class
QHttpMultiPart 类类似于通过 HTTP 发送的 MIME 多部分消息。更多
Header: | #include <QHttpMultiPart> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS Network) target_link_libraries(mytarget PRIVATE Qt6::Network) |
qmake: | QT += network |
继承: | QObject |
- 所有成员(包括继承成员)的列表
- QHttpMultiPart 是网络编程 API 的一部分。
公共类型
enum | ContentType { MixedType, RelatedType, FormDataType, AlternativeType } |
公共函数
QHttpMultiPart(QObject *parent = nullptr) | |
QHttpMultiPart(QHttpMultiPart::ContentType contentType, QObject *parent = nullptr) | |
virtual | ~QHttpMultiPart() |
void | append(const QHttpPart &httpPart) |
QByteArray | boundary() const |
void | setBoundary(const QByteArray &boundary) |
void | setContentType(QHttpMultiPart::ContentType contentType) |
详细说明
QHttpMultiPart 类似于 RFC 2046 中描述的通过 HTTP 发送的 MIME 多部分消息。多部分报文由任意数量的主体部分组成(见QHttpPart ),这些主体部分由一个独特的边界分隔。QHttpMultiPart 的边界是由字符串 "boundary_.oOo._"和随机字符组成的,它提供了足够的唯一性,以确保不会出现在各部分内部。如果需要,还可以通过setBoundary() 设置边界。
举例来说,下面的代码片段构建了一个包含文本部分和图像部分的多部分邮件:
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 和QNetworkAccessManager::post()。
成员类型文档
enum QHttpMultiPart::ContentType
RFC 2046 和其他文件中描述的多部分子类型的已知内容类型列表。
常量 | 值 | 描述 |
---|---|---|
QHttpMultiPart::MixedType | 0 | 对应于 "multipart/mixed "子类型,即主体部分相互独立,如 RFC 2046 所述。 |
QHttpMultiPart::RelatedType | 1 | 对应于 "multipart/related "子类型,即主体部分相互关联,如 RFC 2387 所述。 |
QHttpMultiPart::FormDataType | 2 | 对应于 "multipart/form-data "子类型,即主体部分包含表单元素,如 RFC 2388 所述。 |
QHttpMultiPart::AlternativeType | 3 | 对应于 "multipart/alternative "子类型,即主体部分是相同信息的替代表示,如 RFC 2046 所述。 |
另请参阅 setContentType().
成员函数文档
[explicit]
QHttpMultiPart::QHttpMultiPart(QObject *parent = nullptr)
构建内容类型为MixedType 的 QHttpMultiPart,并将parent 设置为父对象。
另请参阅 QHttpMultiPart::ContentType 。
[explicit]
QHttpMultiPart::QHttpMultiPart(QHttpMultiPart::ContentType contentType, QObject *parent = nullptr)
构造内容类型为contentType 的 QHttpMultiPart,并将父对象设置为父对象。
另请参阅 QHttpMultiPart::ContentType 。
[virtual noexcept]
QHttpMultiPart::~QHttpMultiPart()
销毁多部件。
void QHttpMultiPart::append(const QHttpPart &httpPart)
将httpPart 附加到该多部分。
QByteArray QHttpMultiPart::boundary() const
返回边界。
另请参见 setBoundary().
void QHttpMultiPart::setBoundary(const QByteArray &boundary)
将边界设置为boundary 。
通常情况下,您不需要自己生成边界;在构建边界时,边界会以字符串 "boundary_.oOo._"启动,后面跟着随机字符,并提供足够的唯一性,以确保它不会出现在部件内部。
另请参见 boundary()。
void QHttpMultiPart::setContentType(QHttpMultiPart::ContentType contentType)
将内容类型设为contentType 。通过QNetworkAccessManager::post() 发送多部分报文时,HTTP 头信息部分将使用该内容类型。如果您想使用QHttpMultiPart::ContentType 中未包含的多部分子类型,可以手动在QNetworkRequest 中添加 "Content-Type"(内容类型)标头字段,然后将此请求与多部分报文一起用于发布。
另请参阅 QHttpMultiPart::ContentType 和QNetworkAccessManager::post()。
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.