PDA

View Full Version : QTcpSocket proxy authentication problem



arbi
11th September 2009, 07:53
Hi,

i am trying to make an http request over a proxy server.
The proxy is ISA proxy server. i use QTcpSocket class

the IP of destination : xx.xx.xx.xx port : 80
the IP of proxy server : yy.yy.yy.yy port : 8080

when i write a code like below:


QTcpSocket s;
s.setProxy(QNetworkProxy(QNetworkProxy::DefaultPro xy, "yy.yy.yy.yy",8080));

s.connectToHost(xx.xx.xx.xx,80);

if (!s.waitForConnected(15))
{
throw s.errorString();
}



it never connects, throws exception telling "socket timeout"
I tried setting socket type HttpProxy. it gives exception "Error communicating with HttpProxy"

then i tried to connect proxy server instead of host :


QTcpSocket s;
s.setProxy(QNetworkProxy(QNetworkProxy::DefaultPro xy, "yy.yy.yy.yy",8080));

s.connectToHost(yy.yy.yy.yy,8080);

if (!s.waitForConnected(15))
{
throw s.errorString();
}

i put the information about the host into the http request. and post the request to the proxy server. it worked.
However this only works for anonymous connections.
So the problem occurs when proxy server requires authentication. I wrote the code below:


QTcpSocket s;
s.setProxy(QNetworkProxy(QNetworkProxy::DefaultPro xy, "yy.yy.yy.yy",8080,username,password));

s.connectToHost(yy.yy.yy.yy,8080);

if (!s.waitForConnected(15))
{
throw s.errorString();
}

s.write(requestbytes);
s.waitForReadyRead(-1)
QByteArray response = s.readAll();


the response was like below with status code 407

"HTTP 407 Proxy Authentication Required- The ISA Server requires authorization to fulfill the request
Access to the Web Proxy service is denied
..."

I even used proxyAuthenticationRequired(...) signal and provided the credentials in a slot. but it never enters that slot.
am i missing something ? what else can i do to achieve authentication to the proxy server?

thanks..

wysota
11th September 2009, 09:03
How about using QNetworkAccessManager instead of QTcpSocket?

arbi
11th September 2009, 09:43
yes i tried that class also . but i couldnt get it work. Maybe i am using that wrong.

When using QTcpSocket i connect to the proxy server.


s.connectToHost("yy.yy.yy.yy",8080);// yy.yy.yy.yy:8080 is the proxy address

and send server the message below;

POST http://xx.xx.xx.xx/ HTTP/1.1
User-Agent: TSS Client
Host: xx.xx.xx.xx
Proxy-Connection: Keep-Alive
Content-Length: 61
Content-Type: application/timestamp-query
Identity: 304d0201010410a6ee0e6ddf04fd7e0f36a41708071a850202 07d004105f0ffc985e9c03361f5e11d8bf674e10042007320d c4190c712f1dfb94ced2e983a886674d2a9600ca3fec5e5c56 31e9d73c

<...requestbody...>



how can i do the same with QNetworkAccessManager?
I read the doc about QNetworkAccessManager;
I set the proxy of manager with setProxy() and tried post(...) method with several combinations , connected all of the signals it emits to slots. but couldnt see anything coming from the server.

can you explain how can i use QNetworkAccessManager to make the above request.?
How should i init QNetworkRequest object when posting? should i post to my destination(xx.xx.xx.xx)
or the proxy server(yy.yy.yy.yy).

a piece of code ?

thanks.

bmn
14th September 2009, 16:43
Generally speaking you have to query your destination directly, and set the proxy ip/user/pass in QNetworkProxy .

But..I usually work with QNetworkAccessManager behind a ISA proxy when I'm testing my app at work, and I can't connect to the proxy server directly because...my ISA server uses NTLM v2 hashes and Qt don't support them.
At least until version 4.5 (from QAuthenticator documentation):

QAuthenticator supports the following authentication methods:

Basic
NTLM version 1
Digest-MD5
Note that, in particular, NTLM version 2 is not supported.
To my surprise version 4.5.2 misses this notice, but you can read from google cache: http://209.85.129.132/search?q=cache:pm2Fw4KhqrgJ:doc.trolltech.com/4.5/qauthenticator.html+ntlm+site:doc.trolltech.com&cd=1&hl=it&ct=clnk&gl=it

It doesn't seem that nokia improved qauthenticator (at least there's nothing about it in 4.5.1 and 4.5.2 changelog), and anyway it it still not working for me.

There IS a solution indeed, at least for your development machine: use CNTLM (http://cntlm.sourceforge.net/) on your computer, and point your qt app to the following proxy "localhost:3128" (that is default cntlm port).
Obviously you need to configure and run CNTLM before ;)

Hope this helps