PDA

View Full Version : For help!why QHttp,QNetworkProxy set proxy with username and password failed?



kissfire
11th November 2009, 02:46
Hi everyone:
I dont know why when I set the proxy's username and password,then I test the link,it return failed,but when without username and password,and the proxy is worked,I was fighting with the problem for sometime,but not resolve it yet.My Qt version is 4.5.2.
here is the reference code.can someone give me some suggestions or solus?
//begin mod
QNetworkProxy proxy;
proxy.setType(QNetworkProxy::HttpProxy);
proxy.setHostName(pProxyInfo->qstrProxyServerName);
proxy.setPort(pProxyInfo->qstrProxyPort.toInt());
proxy.setUser(pProxyInfo->qstrProxyUserName);
proxy.setPassword(pProxyInfo->qstrProxyPassWord);
//QNetworkProxy::setApplicationProxy(proxy);*/

m_pTestHttpObj->setProxy(proxy);
Thx and BR

mgoetz
11th November 2009, 08:50
http://doc.trolltech.com/4.5/qhttp.html#proxyAuthenticationRequired

If that does not work either, you need to port your code to QNetworkAccessManager. QHttp is deprecated for Qt 4.6 which comes out in the next weeks.

kissfire
11th November 2009, 09:28
thx,In fact,I had tested it with QNetworkAccessManager before,
1,first,the proxy link says ok,but ,the key is that ,when you analyse the messages sending and receiving,you will find that ,it derectly connect to server and not via proxy pc,
2,second,when QNetworkAccessManager is adapted,and the other operation eg:checking,downloading all failed.
I'am despaired almost...

here is the code with QNetworkAccessManager
//begin mod
QNetworkProxy proxy;
proxy.setType(QNetworkProxy::HttpProxy);
/*proxy.setType(QNetworkProxy::Socks5Proxy);*/
proxy.setHostName(pProxyInfo->qstrProxyServerName);
proxy.setPort(pProxyInfo->qstrProxyPort.toInt());
proxy.setUser(pProxyInfo->qstrProxyUserName);
proxy.setPassword(pProxyInfo->qstrProxyPassWord);
//QNetworkProxy::setApplicationProxy(proxy);*/

m_pTestHttpObj->setProxy(proxy);


/* QNetworkAccessManager a;
a.setProxy(proxy);*/

mgoetz
11th November 2009, 09:34
Even for QNetworkAccessManager, you should take a look at this signal:

http://doc.trolltech.com/4.5/qnetworkaccessmanager.html#proxyAuthenticationRequ ired

kissfire
12th November 2009, 04:01
Hi mgoetz:
I read the article you remended,then I tried ,but still failed,I dont know why,here is the codes,can you kindly give me some suggestions or solutions for proxy(password + username) writted with QNetworkAccessManager?
Thx a lot!
here is the code:
[
QNetworkAccessManager* qnam = new QNetworkAccessManager(this);

connect(qnam, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)), this,
SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)));

QNetworkProxy proxy;
proxy.setType(QNetworkProxy::HttpProxy);
proxy.setHostName(pProxyInfo->qstrProxyServerName);
proxy.setPort(pProxyInfo->qstrProxyPort.toInt());
proxy.setUser(pProxyInfo->qstrProxyUserName);
proxy.setPassword(pProxyInfo->qstrProxyPassWord);

qnam->setProxy(proxy);

mgoetz
12th November 2009, 09:25
Yes :)
You should read about Qt signals and slots. I think you need a slot that sets the authentication parameters.

http://doc.trolltech.com/4.5/signalsandslots.html

kissfire
13th November 2009, 07:21
Add slot and response Func:reponseRecue,but stiil failed ,why???,It seems that i cant get the response message,bcz reponseRecue,not entered,and the dialogue says:detecting...
why???

[
QNetworkAccessManager* qnam = new QNetworkAccessManager(this);


connect(qnam, SIGNAL(finished(QNetworkReply *)), this, SLOT(reponseRecue(QNetworkReply*)));

connect(qnam, SIGNAL(authenticationRequired(QNetworkReply*,QAuth enticator*)),
this, SLOT(authenticate(QNetworkReply*,QAuthenticator*)) );

connect(qnam, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)),
this, SLOT(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)));

//begin mod
QNetworkProxy proxy;
proxy.setType(QNetworkProxy::HttpProxy);
/*proxy.setType(QNetworkProxy::Socks5Proxy);*/
proxy.setHostName(pProxyInfo->qstrProxyServerName);
proxy.setPort(pProxyInfo->qstrProxyPort.toInt());
proxy.setUser(pProxyInfo->qstrProxyUserName);
proxy.setPassword(pProxyInfo->qstrProxyPassWord);
//QNetworkProxy::setApplicationProxy(proxy);*/

/*m_pTestHttpObj->setProxy(proxy);*/
qnam->setProxy(proxy);


....
....
void XXX::reponseRecue(QNetworkReply* reply)
{
CDlgSetting* pSet = (CDlgSetting*)m_pSet;
if (reply->error() == QNetworkReply::NoError)
{
pSet->OnCheckNetConnectSuccess();
qDebug() << reply->readAll();
QMessageBox::information(0, tr("Connection"), tr("ok"));
}
else
{
pSet->OnCheckNetConnectFaild();

QMessageBox::information(0, tr("Connection"), tr("Err !"));
}

m_pSet = NULL;
delete reply;
}

mgoetz
13th November 2009, 08:46
Please look into demos/browser/networkaccessmanager.cpp in the Qt source to see how proxyAuthenticationRequired is used there.

kissfire
16th November 2009, 08:01
QNetworkaccessmanager is worked!
thanks a lot ,mgoetz!
many thanks!

mgoetz
16th November 2009, 08:26
Glad it works:)