在Qt应用程序中共享Cookie的最佳方法 [英] Best way to share cookies within Qt application

查看:301
本文介绍了在Qt应用程序中共享Cookie的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用 QNetworkCookieJar 检索,存储和重新发送 QNetworkManager 的cookie。多个 QNetworkAccessManager 实例可以共享一个 QNetworkCookieJar

I can use QNetworkCookieJar to retrieve, store and resend cookies of a QNetworkManager. Multiple QNetworkAccessManager instances can share a single QNetworkCookieJar.

到目前为止,我已经拥有使用了多个 QNetworkAccessManager 实例,每个类一个(我需要阅读):

So far I have used multiple QNetworkAccessManager instances, one per class (where I need to read):


  1. 所以我可以在1..n QNetworkAccessManager 实例
  2. 中共享一个单例 QNetworkCookieJar
  3. 或最好只有一个单例 QNetworkAccessManager 和一个在所有 QNetworkRequest 中共享一个Jar的情况。 QNetworkAccessManager 作为单个对象是否可行?文档说应该只有一个实例。因此,我最好使用单例 QNetworkAccessManager 吗?

  1. So I could have a singleton QNetworkCookieJar shared across 1..n QNetworkAccessManager instances
  2. Or is it better to have only a singleton QNetworkAccessManager with one Jar shared across all QNetworkRequests. Is QNetworkAccessManager as single object the way to go? Documentation says there should only be one instance. So would I better use a singleton QNetworkAccessManager?

最合适的方法是?

------编辑-------

------ Edit -------

kkoehne的答案据我所知是正确的。这也是文档所说的。但是,在尝试这种方法时,我注意到了两个问题:

kkoehne's answer is correct from what I can tell. Also it is what documentation says. However, when trying this approach I have noticed 2 issues:


  1. 虽然现在我们每个人都有一个 QNetworkAccessManager Web服务,更改为一个实例意味着我需要始终区分我刚刚在完成的网站中收到的内容类型。插槽(从 QNetworkAccessManager :: finished 调用的那个)。那是可行的,但不方便。

  2. 我们在不同的线程中运行读者-不幸的是,我忘记在问题中提及这一点。这使得几乎不可能使用 QNetworkAccessManager 的单个实例,因为成员函数是可重入,但不是线程安全的。 ( ThreadPool中的QNetworkAccessManager

  1. While we have now one QNetworkAccessManager per web service, changing to one single instance means I need to always distinguish what kind of content I just receive in the "finished" slot (the one called from QNetworkAccessManager::finished). That's feasible, but inconvenient.
  2. We run our readers in different threads - I forgot to mention this in the question unfortunately. That makes it almost impossible to use a single instance of QNetworkAccessManager, as the member functions are reentrant, but not thread safe. ( QNetworkAccessManager from ThreadPool )




相关: QNetworkAccessManager是否可能从其他线程获取/发布?

推荐答案

我假设您是指 QNetworkAccessManager ,而不是 QNetworkManager

I assume you're referring to QNetworkAccessManager, not QNetworkManager.

您应该更喜欢在应用程序中使用单个 QNetworkAccessManager 。这样不仅摆脱了同步 QNetworkCookieJar 的任何需求,而且还确保了网络的最佳利用,并且共享了缓存的内容等。

You should prefer having a single QNetworkAccessManager in your application. This not only gets rid of any need to synchronize QNetworkCookieJar's, but also makes sure that the network is best utilized, and that cached content etc is shared.

QtNetworkAccessManager也暗示了这一点,文档

As you noticed yourself, this is also hinted in the QtNetworkAccessManager documentation:


对于整个Qt
应用程序,一个QNetworkAccessManager应该足够。

One QNetworkAccessManager should be enough for the whole Qt application.

这篇关于在Qt应用程序中共享Cookie的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆