user3782412
user3782412

Reputation: 13

Downloading file via HTTPS using QNetworkAccessManager: How to authenticate?

The general answer you can find everywhere is to use the Signal authenticationRequired(QNetworkReply*, QAuthenticator*), then fill the login credentials into the given QAuthenticator object.

However, this does not work in my case as that signal is never emitted. Reason: The server does not return an authorization failure but redirects me to a login page instead. So my program will just download that page.

I have found out how to catch this by checking the attribute QNetworkRequest::RedirectionTargetAttribute of the QNetworkReply. So I can detect the redirection and ask the user for auth info.

But... where do I go from there? How do I set the authentication data? Can I manually set a QAuthenticator to my QNetworkRequest or my QNetworkAccessManager? I didn't find a way to do that anywhere, just via the above-mentioned signal/slot mechanism which does not work because it does not trigger.

Any help would be greatly appreciated!

Upvotes: 1

Views: 732

Answers (1)

user3427419
user3427419

Reputation: 1779

From documentation,

http://qt-project.org/doc/qt-5/qauthenticator.html

QAuthenticator supports the following authentication methods:

  • Basic
  • NTLM version 2
  • Digest-MD5

Since you are getting redirected to a login page, and you haven't indicated if any of the above authentication methods even works, I will assume that it does not because things like Basic authentication is sent on every request to the server. Login pages generally authenticate the client and use some sort of a cookie for future authentication. To do this,

  1. Detect login page
  2. Pass proper credentials to the server (based on what the form wants)
  3. In the QNetworkReply to the login page, look for cookies (Set-Cookie headers).
  4. Pass the relevant cookies back with your requests.
  5. If it works, you are no longer redirected to login page.

For information on cookies, you can get overview via Wikipedia, but for implementation, you need to look at the RFC 6265,

If this is incorrect, and you can use basic authentication, then that information is passed in the URL itself. Set username and password in your QUrl and if it works, you will not be redirected. http://qt-project.org/doc/qt-5/qurl.html#setPassword

Upvotes: 0

Related Questions