PDA

View Full Version : QNetworkAccessManager proxy issue



dholliday
13th January 2010, 13:09
I'm having problems getting through a proxy. If I use the following code.


QWebView a;
a.load(QUrl("http:\\google.co.uk"));
a.show();

The request fails, because I'm behind a proxy. Which is fine and I understand that. So if I amend the code to :


QNetworkProxyFactory::setUseSystemConfiguration ( true) ;
QWebView a;
a.load(QUrl("http:\\google.co.uk"));
a.show();

Everything works fine. I see the google page and I'm not prompted to input my proxy user name or password.
Now this is where I have my problem. I don't want to use a GUI. I'm trying to automate a process at work so I thought I would use QNetworkAccessManager to the same result.

So heres my code :


QUrl a("http://google.co.uk");
QNetworkAccessManager *manager = new QNetworkAccessManager(this);

QNetworkProxyQuery npq(a);
QList<QNetworkProxy> listOfProxies = QNetworkProxyFactory::systemProxyForQuery(npq);

if (listOfProxies.count() !=0){
if (listOfProxies.at(0).type() != QNetworkProxy::NoProxy) {
manager->setProxy(listOfProxies.at(0));
std::cerr << "Using Proxy " << listOfProxies.at(0).hostName().toStdString() << "\n";
}
}

connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(outPutPage(QNetworkReply*)));
manager->get(QNetworkRequest(a));

Now without a proxy this code works fine and I see the HTML printed to the screen. However when running behind a proxy (that requires authentication) I see the code telling me its "Using Proxy" but the code fails even if I supply a valid username and password, it just keeps reporting that the proxy requires authentication. I have tried many different combinations, using QNetworkProxyFactory::setUseSystemConfiguration ( true) ; setting the application proxy.

Does anyone have any idea how I might resolve this. Or is there a class I can use in the QWebKit that doesn't have the over head of rendering. I'm only interested in the data being sent a received. Or doesn't anyone know what the QWebKit class does differently to QNetworkAccessManager::setProxy?

All help will be appreciated.

Regards

Dan

wysota
13th January 2010, 13:49
How do you provide authentication?

dholliday
13th January 2010, 13:54
By trapping the proxyAuthenticationRequired signal and setting the username and password in the SLOT.

But the fact that it asks me for a username and password is confusing me. The proxy is self authenticating, and the QWebKit doesn't ask for the proxy username or password so why should this work any different?

wysota
13th January 2010, 18:28
What do you mean by "self authenticating"? Did you try setting an individual proxy using QNetworkProxy to see if it changes anything?

dholliday
14th January 2010, 10:19
Don't worry about the "Self Authenticating" bit. I just ment in IE i don't have to enter my password because it know who I am, and the QWebView appears to also know who I am.... but thats not really the issue so sorry about that.

Yes I have also created a QNetworkProxy and setting the username, password, type, port and hostname manually and then passing that as an object. But the proxyserverrequiresauthentication signal keeps fireing.
Thanks for you help so far on this. Any other ideas would be great. The only think I can think of is maybe I need to pass the domain name as well as the username. So i tried passing domain\username but it HTML encodes the \ which I don't think the proxy will like.

wysota
14th January 2010, 10:49
Is your application multithreaded?

dholliday
14th January 2010, 15:02
No not at this stage. Do you have any example project that I could download to see if that works though the proxy server?

wysota
14th January 2010, 20:24
No, sorry :(

So let's sum things up... you have code that works when used with QWebView but the same code doesn't work when used with pure QNetworkAccessManager. Is that correct? Can you craft a minimal compilable example reproducing the problem and paste it here?

soxs060389
15th January 2010, 03:16
As far as I know you have to connect a slot to the signal proxyAuthenticationRequired from QNetworkAccessManager. In the connected slot you will have to set the proxy. This settings are remebered according to QtDoc so te signal is only emitted once.
I currently don't have time to write any dummy code.

ansar
15th January 2010, 07:18
In your first code post ...u mentioned that you used QNetworkProxyFactory::setUseSystemConfiguration ( true) ; for QWebView to load the page
behind proxy.But from where u got this setUseSystemConfiguration (true); functionality.
QNetworkProxyFactory not having this...


Please respond... i need this very badly...struggling with proxy problen in QWebView...

dholliday
16th January 2010, 20:09
Thanks wysota.

I've upload two projects which are my prototype projects and are only small. It'd be good to find out if they work behind your proxy. Also ansar make sure you're using the latest version of qt it's defiantly in 4.6 http://qt.nokia.com/doc/4.6/qnetworkproxyfactory.html

4150 and 4151

Thanks again, be good to know if I've done something stupid, or even if they work your end.

Maxbester
21st August 2014, 10:12
It looks good to me.
The only thing, you have to pass login and password to the proxy:


void test::proxyNeedsUserName(QNetworkProxy nr,QAuthenticator* auth){
auth->setUser("yourlogin");
auth->setPassword("yourpassword");
nr.setUser("yourlogin");
nr.setPassword("yourpassword");
}

radmir
13th November 2017, 06:29
Hi, guys,

I think the issue was already solved, but I want to inform what I've found.

In old Qt Versions (up to 4.8.x) signal ProxyAutenticationRequired is emitted every request, and there is no solution but repeatedly send username and password.
Qt 5.3.1 (and may more later's) QNetworkAccessManager keeps user credentials in cache and if they are correct it doesn't emit this signal, if they are not you must answer on that signal, but keep in mind if the site has prefix https, which is usual in today life, the program crashes. This is a bag of Qt 5.3.1. For windows there are patches, but the problem didn't fully solved.

I hope, this info will be useful for you.

Regards,
Radmir

Gregor2
8th May 2019, 11:20
There can also be problems with particular proxy services... Use this source (https://cooltechzone.com/free-vpn-for-android) to test every VPN service, cause I've heard some of them can cause troubles like that.