PDA

View Full Version : Correct usage of QNetworkProxy ?



ralfwickum
15th July 2015, 13:25
Hello,

there is a proxy active and when I want to surf the web, I need to set up proxy settings on my computer, like:
IP 123.234.0.1
Port 12345
User: ralfi
Password: xxx

When I run other programs (for instance JOSM), I just enter IP and port. JOSM then asks me my credentials, if necessary.

In my own application, I also coded a proxy configuration:


QNetworkProxy * proxy = new QNetworkProxy();
//proxy->setType(QNetworkProxy::HttpProxy);
proxy->setHostName(config.value("url").toString()); // "http://123.0.0.1"
proxy->setPort(config.value("port").toUInt()); // 12345

// Credentials required ?
if (config.value("authRequired").toBool()) {
proxy->setUser(config.value("user").toString()); // "testUsr"
proxy->setPassword(config.value("password").toString()); // "test123"
}
As you can see, when I do not have any credentials, no user / password forwarded to the QNetworkProxy. Host and port only.

1. When I do not enter credentials, as above, is there a possibility in QNetworkProxy , to popup a dialog on demand and to ask for credentials ?

2. I set
QNetworkProxy::setApplicationProxy(*proxy); , but that has no effect. I still get a "Couldn't connect to server". What am I missing?

Thanks in advance. Regards Ralf

yeye_olive
15th July 2015, 14:26
1. When I do not enter credentials, as above, is there a possibility in QNetworkProxy , to popup a dialog on demand and to ask for credentials ?
There is no built-in interactive GUI to do that in Qt network classes, if that is what you are looking for. You need to write your own dialog.

If you send requests through QNetworkAccessManager, connect to the proxyAuthenticationRequired() signal; it emitted when the proxy needs authentication. You can open your blocking dialog in the slot (typically by calling QDialog::exec()).

If you had rather set up the proxy before any request is made, just modify your code to show the dialog when config.value("authRequired").toBool() is false.


2. I set
QNetworkProxy::setApplicationProxy(*proxy); , but that has no effect. I still get a "Couldn't connect to server". What am I missing?
It seems to be that your QNetworkProxy has no type. Why did you comment out line 2 in your code snippet?

ralfwickum
17th July 2015, 07:46
There is no built-in interactive GUI to do that in Qt network classes, if that is what you are looking for.
Ok, thanks for that.


It seems to be that your QNetworkProxy has no type. Why did you comment out line 2 in your code snippet?
It was just for testing purposes. It had no effects anyway, since my proxy was not working at all.