QNetworkAccessManager 类

QNetworkAccessManager 类允许应用程序发送网络请求和接收回复。 更多...

头: #include <QNetworkAccessManager>
CMake: find_package(Qt6 COMPONENTS Network REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Network)
qmake: QT += network
继承: QObject

注意: 此类的所有函数 可重入 .

公共类型

enum Operation { HeadOperation, GetOperation, PutOperation, PostOperation, DeleteOperation, CustomOperation }

公共函数

  QNetworkAccessManager (QObject * parent = nullptr)
virtual ~QNetworkAccessManager ()
void addStrictTransportSecurityHosts (const QList<QHstsPolicy> & knownHosts )
bool autoDeleteReplies () const
QAbstractNetworkCache * cache () const
void clearAccessCache ()
void clearConnectionCache ()
void connectToHost (const QString & hostName , quint16 port = 80)
void connectToHostEncrypted (const QString & hostName , quint16 port = 443, const QSslConfiguration & sslConfiguration = QSslConfiguration::defaultConfiguration())
void connectToHostEncrypted (const QString & hostName , quint16 port , const QSslConfiguration & sslConfiguration , const QString & peerName )
QNetworkCookieJar * cookieJar () const
QNetworkReply * deleteResource (const QNetworkRequest & request )
void enableStrictTransportSecurityStore (bool enabled , const QString & storeDir = QString())
QNetworkReply * get (const QNetworkRequest & request )
QNetworkReply * head (const QNetworkRequest & request )
bool isStrictTransportSecurityEnabled () const
bool isStrictTransportSecurityStoreEnabled () const
QNetworkReply * post (const QNetworkRequest & request , QIODevice * data )
QNetworkReply * post (const QNetworkRequest & request , const QByteArray & data )
QNetworkReply * post (const QNetworkRequest & request , QHttpMultiPart * multiPart )
QNetworkProxy proxy () const
QNetworkProxyFactory * proxyFactory () const
QNetworkReply * put (const QNetworkRequest & request , QIODevice * data )
QNetworkReply * put (const QNetworkRequest & request , const QByteArray & data )
QNetworkReply * put (const QNetworkRequest & request , QHttpMultiPart * multiPart )
QNetworkRequest::RedirectPolicy redirectPolicy () const
QNetworkReply * sendCustomRequest (const QNetworkRequest & request , const QByteArray & verb , QIODevice * data = nullptr)
QNetworkReply * sendCustomRequest (const QNetworkRequest & request , const QByteArray & verb , const QByteArray & data )
QNetworkReply * sendCustomRequest (const QNetworkRequest & request , const QByteArray & verb , QHttpMultiPart * multiPart )
void setAutoDeleteReplies (bool shouldAutoDelete )
void setCache (QAbstractNetworkCache * cache )
void setCookieJar (QNetworkCookieJar * cookieJar )
void setProxy (const QNetworkProxy & proxy )
void setProxyFactory (QNetworkProxyFactory * factory )
void setRedirectPolicy (QNetworkRequest::RedirectPolicy policy )
void setStrictTransportSecurityEnabled (bool enabled )
void setTransferTimeout (int timeout = QNetworkRequest::DefaultTransferTimeoutConstant)
QList<QHstsPolicy> strictTransportSecurityHosts () const
virtual QStringList supportedSchemes () const
int transferTimeout () const

信号

void authenticationRequired (QNetworkReply * reply , QAuthenticator * authenticator )
void encrypted (QNetworkReply * reply )
void finished (QNetworkReply * reply )
void preSharedKeyAuthenticationRequired (QNetworkReply * reply , QSslPreSharedKeyAuthenticator * authenticator )
void proxyAuthenticationRequired (const QNetworkProxy & proxy , QAuthenticator * authenticator )
void sslErrors (QNetworkReply * reply , const QList<QSslError> & errors )

保护函数

virtual QNetworkReply * createRequest (QNetworkAccessManager::Operation op , const QNetworkRequest & originalReq , QIODevice * outgoingData = nullptr)

详细描述

网络访问 API 围绕某一 QNetworkAccessManager 对象构造,其保持请求它发送的公共配置和设置。它包含代理 缓存配置、这种问题的相关信号、及可以用于监视网络操作进度的回复信号。一个 QNetworkAccessManager 实例对于整个 Qt 应用程序,应该是足够的。因为 QNetworkAccessManager 基于 QObject ,它只能在其所属的线程中使用。

一旦创建 QNetworkAccessManager 对象,应用程序就可以通过网络使用它发送请求。提供一组接受请求和可选数据的标准函数,且各函数返回 QNetworkReply 对象。返回对象用于获取响应相应请求,返回的任何数据。

完成简单下载网络断开,可以采用:

QNetworkAccessManager *manager = new QNetworkAccessManager(this);
connect(manager, &QNetworkAccessManager::finished,
        this, &MyClass::replyFinished);
manager->get(QNetworkRequest(QUrl("http://qt-project.org")));
					

QNetworkAccessManager 拥有异步 API。当 replyFinished 槽被调用时,它接受的参数是 QNetworkReply 对象,包含下载数据及元数据 (Header 头、等)。

注意: 在请求完成后,用户有责任删除 QNetworkReply 对象,在适当时。不要直接在槽内删除它,因为槽已连接到 finished ()。可以使用 deleteLater () 函数。

注意: QNetworkAccessManager 将它收到的请求排队。并行执行的请求数从属协议。目前,对于桌面平台 HTTP 协议,一个主机/端口的组合可并行执行 6 个请求。

假定管理器已存在,更多涉及范例可以是:

QNetworkRequest request;
request.setUrl(QUrl("http://qt-project.org"));
request.setRawHeader("User-Agent", "MyOwnBrowser 1.0");
QNetworkReply *reply = manager->get(request);
connect(reply, &QIODevice::readyRead, this, &MyClass::slotReadyRead);
connect(reply, &QNetworkReply::errorOccurred,
        this, &MyClass::slotError);
connect(reply, &QNetworkReply::sslErrors,
        this, &MyClass::slotSslErrors);
					

另请参阅 QNetworkRequest , QNetworkReply ,和 QNetworkProxy .

成员类型文档编制

enum QNetworkAccessManager:: Operation

指示此回复正在处理的操作。

常量 描述
QNetworkAccessManager::HeadOperation 1 检索 Header (头) 操作 (创建采用 head ())
QNetworkAccessManager::GetOperation 2 检索 Header (头) 并下载内容 (创建采用 get ())
QNetworkAccessManager::PutOperation 3 上传内容操作 (创建采用 put ())
QNetworkAccessManager::PostOperation 4 经由 HTTP POST 发送用于处理的 HTML 表单内容 (创建采用 post ())
QNetworkAccessManager::DeleteOperation 5 删除内容操作 (创建采用 deleteResource ())
QNetworkAccessManager::CustomOperation 6 自定义操作 (创建采用 sendCustomRequest ())

另请参阅 QNetworkReply::operation ().

成员函数文档编制

QNetworkAccessManager:: QNetworkAccessManager ( QObject * parent = nullptr)

构造 QNetworkAccessManager 对象 (网络访问 API 的中心) 并设置 parent 作为父级对象。

[signal] void QNetworkAccessManager:: authenticationRequired ( QNetworkReply * reply , QAuthenticator * authenticator )

此信号被发射每当最终服务器请求身份验证,在交付请求内容之前。连接到此信号的槽应为内容填充证书(可以确定通过审查 reply 对象) 在 authenticator 对象。

QNetworkAccessManager 会在内部缓存证书并发送相同值,若服务器再次要求身份验证,而不发射 authenticationRequired() 信号。若它拒绝证书,此信号会被再次发射。

注意: 要让请求不发送证书,必须不调用 setUser() 或 setPassword() 在 authenticator 对象。这将导致 finished () 信号被发射采用 QNetworkReply 采用错误 AuthenticationRequiredError .

注意: 使用 QueuedConnection 去连接到此信号是不可能的,因为连接会失败,若身份验证器没有采新信息被填充,当信号返回时。

另请参阅 proxyAuthenticationRequired (), QAuthenticator::setUser (),和 QAuthenticator::setPassword ().

[signal, since 5.1] void QNetworkAccessManager:: encrypted ( QNetworkReply * reply )

此信号被发射当 SSL/TLS 会话已成功完成初始握手时。此时,尚未传输用户数据。可以使用信号以履行额外证书链校验 (例如:通知用户当网站证书改变时)。 reply 参数指定哪个负责网络回复。若回复不匹配期望准则,则应中止它通过调用 QNetworkReply::abort () 通过连接到此信号的槽。可以审查在使用中的 SSL 配置,使用 QNetworkReply::sslConfiguration () 方法。

在内部, QNetworkAccessManager 可能打开到服务器的多个连接,以允许它并行处理请求。这些连接可以被重用,意味着不会发射 encrypted() 信号。这意味着只能保证接收此信号 (对于首次连接到站点而言),在使用期内为 QNetworkAccessManager .

该函数在 Qt 5.1 引入。

另请参阅 QSslSocket::encrypted () 和 QNetworkReply::encrypted ().

[signal] void QNetworkAccessManager:: finished ( QNetworkReply * reply )

此信号被发射每当待决网络回复完成时。 reply 参数将包含指向刚刚完成的回复的指针。此信号被串联发射采用 QNetworkReply::finished () 信号。

QNetworkReply::finished () 了解对象将处于何种状态的有关信息。

注意: 不要删除 reply 对象在连接到此信号的槽中。使用 deleteLater ().

另请参阅 QNetworkReply::finished () 和 QNetworkReply::error ().

[signal, since 5.5] void QNetworkAccessManager:: preSharedKeyAuthenticationRequired ( QNetworkReply * reply , QSslPreSharedKeyAuthenticator * authenticator )

This signal is emitted if the SSL/TLS handshake negotiates a PSK ciphersuite, and therefore a PSK authentication is then required. The reply 对象是 QNetworkReply that is negotiating such ciphersuites.

当使用 PSK 时,客户端必须向服务器发送有效标识和有效 PSK (预共享密钥) 以便 SSL 握手得以继续。应用程序可以在此信号连接的槽中提供此信息,通过填入传递的 authenticator 对象根据需要。

注意: 忽略此信号或未能提供要求证书,将导致握手失败,因此连接将被中止。

注意: authenticator 对象由回复所拥有,且必须不可以通过应用程序被删除。

该函数在 Qt 5.5 引入。

另请参阅 QSslPreSharedKeyAuthenticator .

[signal] void QNetworkAccessManager:: proxyAuthenticationRequired (const QNetworkProxy & proxy , QAuthenticator * authenticator )

此信号被发射每当代理请求身份验证和 QNetworkAccessManager cannot find a valid, cached credential. The slot connected to this signal should fill in the credentials for the proxy proxy authenticator 对象。

QNetworkAccessManager will cache the credentials internally. The next time the proxy requests authentication, QNetworkAccessManager will automatically send the same credential without emitting the proxyAuthenticationRequired signal again.

若代理拒绝证书, QNetworkAccessManager 会再次发射信号。

另请参阅 proxy (), setProxy (),和 authenticationRequired ().

[signal] void QNetworkAccessManager:: sslErrors ( QNetworkReply * reply , const QList < QSslError > & errors )

此信号被发射,若 SSL/TLS 会话在设置期间遇到错误 (包括证书验证错误)。 errors parameter contains the list of errors and reply QNetworkReply that is encountering these errors.

为指示错误不致命,且连接应继续进行, QNetworkReply::ignoreSslErrors () 函数应该被调用,从连接到此信号的槽中。若不调用,SSL 会话将被断开,在交换任何数据 (包括 URL) 之前。

This signal can be used to display an error message to the user indicating that security may be compromised and display the SSL settings (see sslConfiguration() to obtain it). If the user decides to proceed after analyzing the remote certificate, the slot should call ignoreSslErrors().

另请参阅 QSslSocket::sslErrors (), QNetworkReply::sslErrors (), QNetworkReply::sslConfiguration (),和 QNetworkReply::ignoreSslErrors ().

[虚拟] QNetworkAccessManager:: ~QNetworkAccessManager ()

销毁 QNetworkAccessManager 对象并释放任何资源。注意 QNetworkReply objects that are returned from this class have this object set as their parents, which means that they will be deleted along with it if you don't call QObject::setParent () on them.

[since 5.9] void QNetworkAccessManager:: addStrictTransportSecurityHosts (const QList < QHstsPolicy > & knownHosts )

将 HSTS (HTTP 严格传输安全) 策略添加到 HSTS 缓存。 knownHosts contains the known hosts that have QHstsPolicy information.

注意: An expired policy will remove a known host from the cache, if previously present.

注意: 当处理 HTTP 响应, QNetworkAccessManager can also update the HSTS cache, removing or updating exitsting policies or introducing new knownHosts . The current implementation thus is server-driven, client code can provide QNetworkAccessManager with previously known or discovered policies, but this information can be overridden by "Strict-Transport-Security" response headers.

该函数在 Qt 5.9 引入。

另请参阅 addStrictTransportSecurityHosts(), enableStrictTransportSecurityStore (),和 QHstsPolicy .

[since 5.14] bool QNetworkAccessManager:: autoDeleteReplies () const

返回 true 若 QNetworkAccessManager 目前被配置为自动删除 QNetworkReplies,否则 false。

该函数在 Qt 5.14 引入。

另请参阅 setAutoDeleteReplies and QNetworkRequest::AutoDeleteReplyOnFinishAttribute .

QAbstractNetworkCache *QNetworkAccessManager:: cache () const

返回用于存储从网络获得数据的缓存。

另请参阅 setCache ().

[since 5.0] void QNetworkAccessManager:: clearAccessCache ()

刷新身份验证数据和网络连接的内部缓存。

此函数对履行自动测试很有用。

该函数在 Qt 5.0 引入。

另请参阅 clearConnectionCache ().

[since 5.9] void QNetworkAccessManager:: clearConnectionCache ()

刷新网络连接的内部缓存。相比 clearAccessCache () 保留身份验证数据。

该函数在 Qt 5.9 引入。

另请参阅 clearAccessCache ().

[since 5.2] void QNetworkAccessManager:: connectToHost (const QString & hostName , quint16 port = 80)

初启连接到主机,给定通过 hostName 在端口 port 。此函数对在发出 HTTP 请求之前完成与主机的 TCP 握手很有用,结果是降低网络延迟。

注意: 此函数没有可能去报告错误。

该函数在 Qt 5.2 引入。

另请参阅 connectToHostEncrypted (), get (), post (), put (),和 deleteResource ().

[since 5.2] void QNetworkAccessManager:: connectToHostEncrypted (const QString & hostName , quint16 port = 443, const QSslConfiguration & sslConfiguration = QSslConfiguration::defaultConfiguration())

初启连接到主机,给定通过 hostName 在端口 port ,使用 sslConfiguration . This function is useful to complete the TCP and SSL handshake to a host before the HTTPS request is made, resulting in a lower network latency.

注意: Preconnecting a HTTP/2 connection can be done by calling setAllowedNextProtocols() on sslConfiguration with QSslConfiguration::ALPNProtocolHTTP2 contained in the list of allowed protocols. When using HTTP/2, one single connection per host is enough, i.e. calling this method multiple times per host will not result in faster network transactions.

注意: 此函数没有可能去报告错误。

该函数在 Qt 5.2 引入。

另请参阅 connectToHost (), get (), post (), put (),和 deleteResource ().

[since 5.13] void QNetworkAccessManager:: connectToHostEncrypted (const QString & hostName , quint16 port , const QSslConfiguration & sslConfiguration , const QString & peerName )

这是重载函数。

初启连接到主机,给定通过 hostName 在端口 port ,使用 sslConfiguration with peerName set to be the hostName used for certificate validation. This function is useful to complete the TCP and SSL handshake to a host before the HTTPS request is made, resulting in a lower network latency.

注意: Preconnecting a HTTP/2 connection can be done by calling setAllowedNextProtocols() on sslConfiguration with QSslConfiguration::ALPNProtocolHTTP2 contained in the list of allowed protocols. When using HTTP/2, one single connection per host is enough, i.e. calling this method multiple times per host will not result in faster network transactions.

注意: 此函数没有可能去报告错误。

该函数在 Qt 5.13 引入。

另请参阅 connectToHost (), get (), post (), put (),和 deleteResource ().

QNetworkCookieJar *QNetworkAccessManager:: cookieJar () const

返回 QNetworkCookieJar 用于存储从网络获得的 Cookie 及即将被发送的 Cookie。

另请参阅 setCookieJar ().

[virtual protected] QNetworkReply *QNetworkAccessManager:: createRequest ( QNetworkAccessManager::Operation op , const QNetworkRequest & originalReq , QIODevice * outgoingData = nullptr)

返回新的 QNetworkReply 对象以处理操作 op 和请求 originalReq 。设备 outgoingData 始终为 0 对于 Get 和 Head 请求而言,但是值被传递给 post () 和 put () 在这些操作中 ( QByteArray 变体会传递 QBuffer 对象)。

默认实现调用 QNetworkCookieJar::cookiesForUrl () 当 Cookie Jar 设置采用 setCookieJar () 以获得要被发送给远程服务器的 Cookie。

返回的对象必须处于打开状态。

QNetworkReply *QNetworkAccessManager:: deleteResource (const QNetworkRequest & request )

发送请求以删除资源标识通过 URL 的 request .

注意: 此特征目前只可用于 HTTP,履行 HTTP DELETE 请求。

另请参阅 get (), post (), put (),和 sendCustomRequest ().

[since 5.10] void QNetworkAccessManager:: enableStrictTransportSecurityStore ( bool enabled , const QString & storeDir = QString())

enabled is true ,内部 HSTS (HTTP 严格传输安全) 缓存将使用持久存储来读写 HSTS 策略。 storeDir defines where this store will be located. The default location is defined by QStandardPaths::CacheLocation . If there is no writable QStandartPaths::CacheLocation and storeDir is an empty string, the store will be located in the program's working directory.

注意: If HSTS cache already contains HSTS policies by the time persistent store is enabled, these policies will be preserved in the store. In case both cache and store contain the same known hosts, policies from cache are considered to be more up-to-date (and thus will overwrite the previous values in the store). If this behavior is undesired, enable HSTS store before enabling Strict Transport Security. By default, the persistent store of HSTS policies is disabled.

该函数在 Qt 5.10 引入。

另请参阅 isStrictTransportSecurityStoreEnabled (), setStrictTransportSecurityEnabled (),和 QStandardPaths::standardLocations ().

QNetworkReply *QNetworkAccessManager:: get (const QNetworkRequest & request )

张贴请求以获取内容对于目标 request 并返回新的 QNetworkReply 对象并打开为读取发射的 readyRead() 信号,每当新数据到达时。

会下载内容及关联 Header (头)。

另请参阅 post (), put (), deleteResource (),和 sendCustomRequest ().

QNetworkReply *QNetworkAccessManager:: head (const QNetworkRequest & request )

张贴请求以获取网络头为 request 并返回新的 QNetworkReply 对象 (将包含这种 Header 头)。

函数以 HTTP 请求关联 HEAD 头命名。

[since 5.9] bool QNetworkAccessManager:: isStrictTransportSecurityEnabled () const

返回 true,若 HSTS (HTTP 严格传输安全) 被启用。默认情况下,HSTS 是禁用的。

该函数在 Qt 5.9 引入。

另请参阅 setStrictTransportSecurityEnabled ().

[since 5.10] bool QNetworkAccessManager:: isStrictTransportSecurityStoreEnabled () const

返回 true,若 HSTS (HTTP 严格传输安全) 缓存使用永久存储去加载、存储 HSTS 策略。

该函数在 Qt 5.10 引入。

另请参阅 enableStrictTransportSecurityStore ().

QNetworkReply *QNetworkAccessManager:: post (const QNetworkRequest & request , QIODevice * data )

把 HTTP POST (张贴) 请求发送给指定目的地通过 request 并返回新的 QNetworkReply 为读取而打开的对象,将包含由服务器所发送的回复。内容对于 data 设备将上传到服务器。

data 必须被打开以供读取且必须保持有效,直到 finished () 信号被发射为此回复。

注意: 发送除 HTTP HTTPS 协议的 POST (张贴) 请求是未定义的,且可能失败。

另请参阅 get (), put (), deleteResource (),和 sendCustomRequest ().

QNetworkReply *QNetworkAccessManager:: post (const QNetworkRequest & request , const QByteArray & data )

这是重载函数。

发送内容为 data 字节数组到目的地指定通过 request .

QNetworkReply *QNetworkAccessManager:: post (const QNetworkRequest & request , QHttpMultiPart * multiPart )

这是重载函数。

发送内容为 multiPart 消息到目的地指定通过 request .

这可以被用于通过 HTTP 发送 MIME 多部分消息。

另请参阅 QHttpMultiPart , QHttpPart ,和 put ().

QNetworkProxy QNetworkAccessManager:: proxy () const

返回 QNetworkProxy 请求发送使用此 QNetworkAccessManager 对象将使用。代理的默认值为 QNetworkProxy::DefaultProxy .

另请参阅 setProxy (), setProxyFactory (),和 proxyAuthenticationRequired ().

QNetworkProxyFactory *QNetworkAccessManager:: proxyFactory () const

返回代理工厂,此 QNetworkAccessManager 对象被用于确定要被用于请求的代理。

注意:由此函数返回的指针的管理是通过 QNetworkAccessManager 且可以在任何时候被删除。

另请参阅 setProxyFactory () 和 proxy ().

QNetworkReply *QNetworkAccessManager:: put (const QNetworkRequest & request , QIODevice * data )

上传内容为 data 到目的地 request 并返回新的 QNetworkReply 对象会被打开为回复。

data 必须被打开以供读取,当此函数被调用时;且必须保持有效直到 finished () 信号被发射为此回复。

返回对象是否有任何东西可供读取,取决于协议。对于 HTTP,服务器可能发送指示上传成功 (或不成功) 的小 HTML 页面。其它协议可能会在其回复中有内容。

注意: 对于 HTTP,此请求将发送 PUT 请求 (大多数服务器不允许)。表单上传机制,包括透过 HTML 表单上传文件的机制,使用 POST 机制。

另请参阅 get (), post (), deleteResource (),和 sendCustomRequest ().

QNetworkReply *QNetworkAccessManager:: put (const QNetworkRequest & request , const QByteArray & data )

这是重载函数。

发送内容为 data 字节数组到目的地指定通过 request .

QNetworkReply *QNetworkAccessManager:: put (const QNetworkRequest & request , QHttpMultiPart * multiPart )

这是重载函数。

发送内容为 multiPart 消息到目的地指定通过 request .

这可以被用于通过 HTTP 发送 MIME 多部分消息。

另请参阅 QHttpMultiPart , QHttpPart ,和 post ().

[since 5.9] QNetworkRequest::RedirectPolicy QNetworkAccessManager:: redirectPolicy () const

返回所用的重定向策略,当创建新请求时。

该函数在 Qt 5.9 引入。

另请参阅 setRedirectPolicy () 和 QNetworkRequest::RedirectPolicy .

QNetworkReply *QNetworkAccessManager:: sendCustomRequest (const QNetworkRequest & request , const QByteArray & verb , QIODevice * data = nullptr)

向服务器发送自定义请求标识通过 URL 的 request .

用户负责发送 verb 给根据 HTTP 规范有效的服务器。

This method provides means to send verbs other than the common ones provided via get () 或 post () etc., for instance sending an HTTP OPTIONS command.

data is not empty, the contents of the data device will be uploaded to the server; in that case, data must be open for reading and must remain valid until the finished () 信号被发射为此回复。

注意: 此特征目前只可用于 HTTP(S)。

另请参阅 get (), post (), put (),和 deleteResource ().

[since 5.8] QNetworkReply *QNetworkAccessManager:: sendCustomRequest (const QNetworkRequest & request , const QByteArray & verb , const QByteArray & data )

这是重载函数。

发送内容为 data 字节数组到目的地指定通过 request .

该函数在 Qt 5.8 引入。

[since 5.8] QNetworkReply *QNetworkAccessManager:: sendCustomRequest (const QNetworkRequest & request , const QByteArray & verb , QHttpMultiPart * multiPart )

这是重载函数。

向服务器发送自定义请求标识通过 URL 的 request .

发送内容为 multiPart 消息到目的地指定通过 request .

这可用于为自定义 verb 发送 MIME 多部分消息。

该函数在 Qt 5.8 引入。

另请参阅 QHttpMultiPart , QHttpPart ,和 put ().

[since 5.14] void QNetworkAccessManager:: setAutoDeleteReplies ( bool shouldAutoDelete )

启用 (或禁用) 自动删除 QNetworkReplies .

设置 shouldAutoDelete to true is the same as setting the QNetworkRequest::AutoDeleteReplyOnFinishAttribute attribute to true on all future QNetworkRequests passed to this instance of QNetworkAccessManager unless the attribute was already explicitly set on the QNetworkRequest .

该函数在 Qt 5.14 引入。

另请参阅 autoDeleteReplies and QNetworkRequest::AutoDeleteReplyOnFinishAttribute .

void QNetworkAccessManager:: setCache ( QAbstractNetworkCache * cache )

将管理器的网络缓存设为 cache 指定。缓存用于由管理器分派的所有请求。

Use this function to set the network cache object to a class that implements additional features, like saving the cookies to permanent storage.

注意: QNetworkAccessManager 拥有所有权对于 cache 对象。

QNetworkAccessManager by default does not have a set cache. Qt provides a simple disk cache, QNetworkDiskCache , which can be used.

另请参阅 cache () 和 QNetworkRequest::CacheLoadControl .

void QNetworkAccessManager:: setCookieJar ( QNetworkCookieJar * cookieJar )

将管理器的 Cookie Jar 设为 cookieJar 指定。Cookie Jar 用于由管理器分派的所有请求。

Use this function to set the cookie jar object to a class that implements additional features, like saving the cookies to permanent storage.

注意: QNetworkAccessManager 拥有所有权对于 cookieJar 对象。

cookieJar is in the same thread as this QNetworkAccessManager , it will set the parent of the cookieJar so that the cookie jar is deleted when this object is deleted as well. If you want to share cookie jars between different QNetworkAccessManager objects, you may want to set the cookie jar's parent to 0 after calling this function.

QNetworkAccessManager by default does not implement any cookie policy of its own: it accepts all cookies sent by the server, as long as they are well formed and meet the minimum security requirements (cookie domain matches the request's and cookie path matches the request's). In order to implement your own security policy, override the QNetworkCookieJar::cookiesForUrl () 和 QNetworkCookieJar::setCookiesFromUrl () virtual functions. Those functions are called by QNetworkAccessManager when it detects a new cookie.

另请参阅 cookieJar (), QNetworkCookieJar::cookiesForUrl (),和 QNetworkCookieJar::setCookiesFromUrl ().

void QNetworkAccessManager:: setProxy (const QNetworkProxy & proxy )

将用于未来请求的代理设为 proxy 。这不会影响已经发送的请求。 proxyAuthenticationRequired () 信号会被发射若代理请求身份验证。

A proxy set with this function will be used for all requests issued by QNetworkAccessManager . In some cases, it might be necessary to select different proxies depending on the type of request being sent or the destination host. If that's the case, you should consider using setProxyFactory ().

另请参阅 proxy () 和 proxyAuthenticationRequired ().

void QNetworkAccessManager:: setProxyFactory ( QNetworkProxyFactory * factory )

将此类的代理工厂设为 factory . A proxy factory is used to determine a more specific list of proxies to be used for a given request, instead of trying to use the same proxy value for all requests.

All queries sent by QNetworkAccessManager will have type QNetworkProxyQuery::UrlRequest .

例如,代理工厂可以应用以下规则:

  • if the target address is in the local network (for example, if the hostname contains no dots or if it's an IP address in the organization's range), return QNetworkProxy::NoProxy
  • 若请求为 FTP,返回 FTP 代理
  • 若请求为 HTTP 或 HTTPS,返回 HTTP 代理
  • 否则,返回 SOCKSv5 代理服务器

The lifetime of the object factory will be managed by QNetworkAccessManager 。它会删除对象,当有必要时。

注意: 若指定代理的设置是采用 setProxy (),工厂将不会被使用。

另请参阅 proxyFactory (), setProxy (),和 QNetworkProxyQuery .

[since 5.9] void QNetworkAccessManager:: setRedirectPolicy ( QNetworkRequest::RedirectPolicy policy )

Sets the manager's redirect policy to be the policy specified. This policy will affect all subsequent requests created by the manager.

使用此函数在管理器级别启用或禁用 HTTP 重定向。

注意: When creating a request QNetworkRequest::RedirectAttributePolicy has the highest priority, next by priority the manager's policy.

默认值为 QNetworkRequest::NoLessSafeRedirectPolicy . Clients relying on manual redirect handling are encouraged to set this policy explicitly in their code.

该函数在 Qt 5.9 引入。

另请参阅 redirectPolicy () 和 QNetworkRequest::RedirectPolicy .

[since 5.9] void QNetworkAccessManager:: setStrictTransportSecurityEnabled ( bool enabled )

enabled is true , QNetworkAccessManager follows the HTTP Strict Transport Security policy (HSTS, RFC6797). When processing a request, QNetworkAccessManager automatically replaces the "http" scheme with "https" and uses a secure transport for HSTS hosts. If it's set explicitly, port 80 is replaced by port 443.

When HSTS is enabled, for each HTTP response containing HSTS header and received over a secure transport, QNetworkAccessManager will update its HSTS cache, either remembering a host with a valid policy or removing a host with an expired or disabled HSTS policy.

该函数在 Qt 5.9 引入。

另请参阅 isStrictTransportSecurityEnabled ().

[since 5.15] void QNetworkAccessManager:: setTransferTimeout ( int timeout = QNetworkRequest::DefaultTransferTimeoutConstant)

设置 timeout 作为传输超时 (以毫秒为单位)。

Transfers are aborted if no bytes are transferred before the timeout expires. Zero means no timer is set. If no argument is provided, the timeout is QNetworkRequest::DefaultTransferTimeoutConstant . If this function is not called, the timeout is disabled and has the value zero. The request-specific non-zero timeouts set for the requests that are executed override this value. This means that if QNetworkAccessManager has an enabled timeout, it needs to be disabled to execute a request without a timeout.

该函数在 Qt 5.15 引入。

另请参阅 transferTimeout ().

[since 5.9] QList < QHstsPolicy > QNetworkAccessManager:: strictTransportSecurityHosts () const

Returns the list of HTTP Strict Transport Security policies. This list can differ from what was initially set via addStrictTransportSecurityHosts () if HSTS cache was updated from a "Strict-Transport-Security" response header.

该函数在 Qt 5.9 引入。

另请参阅 addStrictTransportSecurityHosts () 和 QHstsPolicy .

[virtual, since 5.2] QStringList QNetworkAccessManager:: supportedSchemes () const

列出由访问管理器支持的所有 URL 方案。

Reimplement this method to provide your own supported schemes in a QNetworkAccessManager subclass. It is for instance necessary when your subclass provides support for new protocols.

该函数在 Qt 5.2 引入。

[since 5.15] int QNetworkAccessManager:: transferTimeout () const

返回用于传输的超时 (以毫秒为单位)。

此超时为 0 若 setTransferTimeout () 没有被调用,这意味着不使用超时。

该函数在 Qt 5.15 引入。

另请参阅 setTransferTimeout ().