0

I try use QNetworkAccessManager to save server cookie like chrome browser, i follow some article and had write the follow demo.

Code:

from PyQt5.QtCore import *
from PyQt5.QtNetwork import *

class NetworkManager:
    def __init__(self):
        self.nam = QNetworkAccessManager()
        self.nam.setAutoDeleteReplies(True)
        self.nam.finished.connect(self.on_finished)
        self.cookieJar = QNetworkCookieJar()
        self.nam.setCookieJar(self.cookieJar)


    def on_finished(self, reply: QNetworkReply):

        if reply.error() == QNetworkReply.NoError:
            data = reply.readAll()
            cookies = reply.header(QNetworkRequest.SetCookieHeader)
            if cookies is not None:
                self.cookieJar.setCookiesFromUrl(cookies, reply.url())

        print(self.nam.cookieJar().allCookies())
    

app = QCoreApplication([])
nm = NetworkManager()

url = 'sampleurl'
req = QNetworkRequest(QUrl(url))

nm.nam.post(req, b'name=cc&qq=123')
app.exec()

The issue is that i had save server cookie and try to request the same url or other url, i found the request header don't have set-cookie header, is i use it a wrong way?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
jett chen
  • 1,067
  • 16
  • 33
  • 1
    mmmm, could you explain better how you can see that it doesn't work – eyllanesc Jun 29 '20 at 13:58
  • I had read [QT HTTP Post issue when server requires cookies](https://stackoverflow.com/questions/4509441/qt-http-post-issue-when-server-requires-cookies) article, maybe i need to fetch cookie from cookiejar and then set request cookie header. – jett chen Jul 01 '20 at 06:56
  • You have not answered my question, I want a method that allows me to reproduce your problem – eyllanesc Jul 01 '20 at 12:49
  • @eyllanesc I think the `QNetworkCookieJar` will auto set request cookie header that i had save in `QNetworkCookieJar`, but when i read other article, i need to fetch cookie from url and then manually set request cookie header, maybe i use it in a wrong way formerly. – jett chen Jul 02 '20 at 01:23

0 Answers0