Reputation: 7418
I'm working with a webservice which requires a valid username/password. From PyQt, I'm accessing the webservice using QNetworkAccessManager which emits the
authenticationRequired (QNetworkReply*, QAuthenticator*)
signal when (obviously), authentication is required. When I fill in the user and psswd for QAuthenticator, everything works fine. However, I can't see how to break the loop when the user is not valid.
From the docs for authenticationRequired:
"If it rejects the credentials, this signal will be emitted again."
For invalid credentials this signal is emitted again, and again, and again... Looking at the error code in the reply showed 0. How is this loop supposed to be broken or handled so that it terminates with an error?
Upvotes: 4
Views: 3356
Reputation: 638
If you return from a slot connected to &QNetworkAccessManager::proxyAuthenticationRequired without calling any QAuthenticator method, the request will be aborted with real HTTP code (e.g. 401 or 407).
For instance you can call QAuthenticator::setUser and setPassword when the user clics OK and call nothing when he clics Cancel. That's all :-)
This is also true for slots connected to &QNetworkAccessManager::authenticationRequired (where you do not have to call QNetworkReply::abort(), you can simply call no QAuthenticator method at all, which preserve HTTP code too, whereas calling abort() alter it).
Upvotes: 0
Reputation: 4259
Calling QNetworkReply::abort()
will cause the request to fail with the error 'Operation Aborted' instead of the original 401 error. The correct thing to do now seems to be not to call QAuthenticator::setUser()
or QAuthenticator::setPassword()
inside your authenticationRequired()
slot. This will leave the QAuthenticatorPrivate::phase
as Done
which will cause the request to terminate cleanly with the correct error code.
The Qt documentation is rather unclear on this point.
This did not seem to be the behaviour in Qt 4.7, and was introduced at some point in Qt 4.8.
Upvotes: 0
Reputation: 7705
What about trying:
QNetworkReply::abort()
or
QNetworkReply::close()
?
Upvotes: 1
Reputation: 9814
Yeh, it's odd. What I've done previously is check if I've already authenticated with those details and if I have then call QNetworkReply.abort()
Upvotes: 3