View Full Version : I get only 1 of 4 cookies...
cherva
2nd May 2009, 11:12
I'm trying to get a web page that returns to me 4 cookies with this part of code:
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
QNetworkRequest request;
request.setUrl(QUrl(line));
request.setRawHeader("User-Agent", "Firefox/3.0.10");
reply = manager->get(request);
connect(reply, SIGNAL(finished()), this, SLOT(slotReadyRead()));
void Window::slotReadyRead(){
QList<QByteArray> headers=reply->rawHeaderList();
QVariant cookies=reply->header(QNetworkRequest::SetCookieHeader);
wget = reply->readAll();
QList<QVariant> list = cookies.toList();
qDebug()<<"HEADERS: "<< headers[2] << " COOKIES: " << list.size();
qDebug()<<"COOKIES FROM rawHEADER: " << reply->rawHeader("Set-Cookie") ;
...
...
...
The output of the program is:
HEADERS: "Set-Cookie" COOKIES: 0
COOKIES FROM rawHEADER: "JSESSIONID=F9C30312025976F6A8A4813CC96B8E65; Path=/app"
The cookies from rawHEADER is only 1 ... the server sends 4... what is wrong ?
e8johan
4th May 2009, 08:55
How does the raw header list look if you iterate over it and print it, e.g.
foreach( QByteArray b, reply->rawHeaderList() )
qDebug() << b;
Perhaps there are several set cookie entries....
cherva
4th May 2009, 13:54
OUTPUT:
B: "Date"
B: "Server"
B: "Set-Cookie"
B: "Vary"
B: "Content-Encoding"
B: "Pragma"
B: "Cache-Control"
B: "Connection"
B: "Content-Type"
HEADERS: "Set-Cookie" COOKIES: 0
COOKIES FROM rawHEADER: "JSESSIONID=669579C9DD7AE23FFA6EEA7FF9E30E7C; Path=/app"
e8johan
4th May 2009, 13:57
Have you used wireshark or something similar to verify that you actually do get four cookies?
cherva
4th May 2009, 14:12
Yes, Wireshark sends me 4 cookies:
Transmission Control Protocol, Src Port: http (80), Dst Port: 41003 (41003), Seq: 1, Ack: 178, Len: 580
Hypertext Transfer Protocol
HTTP/1.1 302 Moved Temporarily\r\n
Date: Mon, 04 May 2009 13:09:58 GMT\r\n
Server: Apache\r\n
Set-Cookie: JSESSIONID=4D6B05D58928843F51D03F6D85ED5F40; Path=/app\r\n
SET-COOKIE: _key=7710db16cea77c8152a557a0ab0469cc; Path=/app; HTTPOnly\r\n
Set-Cookie: uhost=mail33.abv.bg; Domain=.abv.bg; Path=/\r\n
Set-Cookie: usid=4D6B05D58928843F51D03F6D85ED5F40; Domain=.abv.bg; Path=/\r\n
Location: http://mail33.abv.bg/app/j/home.jsp\r\n
Content-Length: 0\r\n
Pragma: no-cache\r\n
Cache-Control: no-cache\r\n
Vary: User-Agent\r\n
Keep-Alive: timeout=5, max=100\r\n
Connection: Keep-Alive\r\n
Content-Type: text/xml;charset=utf-8\r\n
The strange thing is that the output of the program was:
COOKIES FROM rawHEADER: "JSESSIONID=D106ADD7E19F1913E020E542B8C73EFB; Path=/app"
the cookie is not the same :confused:
e8johan
4th May 2009, 14:18
Comparing your wireshark output to the output from the foreach-loop described earlier, you seem to miss other entries as well.
What do you get from reply.readAll() ?
cherva
4th May 2009, 14:22
I get the page that I should get when I login with wrong user or password or in my case with wrong cookies....
e8johan
4th May 2009, 14:26
Does that mean that you have to add something to your request? Or do you mean that you get too few cookies but expect it to work?
cherva
4th May 2009, 14:28
I need all four cookies it won't work with just one.
e8johan
4th May 2009, 14:46
How does the QNetworkAccessManager's QNetworkCookieJar look?
cherva
4th May 2009, 14:52
Can you tell me how to check because allCookies() is protected and I don't want to subclass the whole class.... :rolleyes:
e8johan
4th May 2009, 14:54
I'd go or the cookiesForUrl function.
cherva
4th May 2009, 15:03
I inserted
QList<QNetworkCookie> jar=manager->cookieJar()->cookiesForUrl(QUrl(line)); on line 7
and in debug mode jar stayed empty until the program ended.
e8johan
4th May 2009, 15:08
How does the original URL look? Do you try to login here, or do you try to access some location where you already are logged in? If so, how did you originally log in there?
You say that you reach the login/wrong password page, and that seems reasonable to me if you don't have any cookies...
cherva
4th May 2009, 15:19
Here is the deal:
1) I send the user and password to the server that the original http://abv.bg 's form will send the data.
2) The server returnes me "HTTP 302 Moved temporarily" and a link like:
http://mailXX.abv.bg/servlet/plogin?s=hewirhwekrjhwekjrhw.passportX" wher re X's are random nubers.
3) When I go to that page I get the same HTTP 302 Moved temprarily message but with differtent mailXX.
4) I go there and this page gives me 4 cookies: example:
Set-Cookie: JSESSIONID=4D6B05D58928843F51D03F6D85ED5F40; Path=/app\r\n
SET-COOKIE: _key=7710db16cea77c8152a557a0ab0469cc; Path=/app; HTTPOnly\r\n
Set-Cookie: uhost=mail33.abv.bg; Domain=.abv.bg; Path=/\r\n
Set-Cookie: usid=4D6B05D58928843F51D03F6D85ED5F40; Domain=.abv.bg; Path=/\r\n
and redirects me to "http://mailXX.abv.bg/app/j/home.jsp" whre the XX are from the last page . ( the page that gave me the cookies)
This is what I need. I used WGET to deal with the cookies and everything is ok, but my program reached version 1.00 and I want to remove it because it's stupid for the windows version to come with a hidden wget.exe in the same folder.
wysota
4th May 2009, 15:51
In general I'd say that what the server returns you could be a bit invalid. From what I understand (I could be wrong though) there should only be one header of each type therefore you only get one set-cookie header that contains all cookies that are to be set separated by semicolons.
QNetworkReply::header() should return you a QVariant when passed QNetworkRequest::SetCookieHeader. This variant is really QList<QNetworkCookie>. When you get hold of it, you can list its contents to check out what cookies you are set. Then you can store them in your cookie-jar.
Be sure to cast the variant to a proper type or else you will get an empty list like you do now.
cherva
4th May 2009, 16:00
I tried that on line 12 and 14 on the first post and "list" is empty.
wysota
4th May 2009, 17:46
I tried that on line 12 and 14 on the first post and "list" is empty.
Read again my last sentence and see what QVariant::toList() returns.
cherva
4th May 2009, 18:19
QVariant cookies=reply->header(QNetworkRequest::SetCookieHeader);
QList<QVariant> list = cookies.toList();
Makes cookies = QList<QNetworkCookie> and list = 0 items
wysota
4th May 2009, 18:24
No, it makes it QList<QVariant> and not QList<QNetworkCookie> thus the size is 0 (because it is not a QList<QVariant>).
cherva
4th May 2009, 18:28
Aaaammm "No, it makes it QList<QVariant>............ (because it is not a QList<QVariant>)." ?
wysota
4th May 2009, 18:41
Aaaammm "No, it makes it QList<QVariant>............ (because it is not a QList<QVariant>)." ?
I suggest you rephrase this post because I have no idea what you mean. If it's that you don't see a difference between QVariant and QNetworkCookie then I suggest you click both links and read the docs to see the differences. You are trying to convert a QVariant holding a QList<QNetworkCookie> into QList<QVariant> which it is not thus you get an empty QList<QVariant>. It's like you wanted to convert a rectangle into a circle - you'd get an invalid circle because rectangles can't be converted to circles without actually knowing what a "rectangles" and "circles" are.
cherva
4th May 2009, 21:53
I didn't understand what he posted.... at firs he says that it's a QList<QVariant> and then in the brackets he says that it's not QList<QVariant>, but never mind that. I don't know how to convert it to a QList<QNetworkCookie> ? I guess not like
QList<QNetworkCookie> list = cookies.toList(); :confused:
wysota
4th May 2009, 22:36
at firs he says that it's a QList<QVariant>
No, at first "he" says the cast tries to make it QList<QVariant>.
and then in the brackets he says that it's not QList<QVariant>,
Yes, so "he" says.
but never mind that.
Actually you should mind because it is the most important part of the whole discussion.
I don't know how to convert it to a QList<QNetworkCookie> ? I guess not like
QList<QNetworkCookie> list = cookies.toList(); :confused:
I guess the compiler already told you it is not the right route...
Try this:
QList<QNetworkCookie> cookieList = qvariant_cast<QList<QNetworkCookie> >(cookies);
cherva
4th May 2009, 23:20
I don't know how to explain what happened...
My cookieList has 1 item: "d" wich is a QSharedDataPointer<QNetworkCookiePrivate> wich has another "d" wich is QNetworkCookiePrivate * with value 0x1 there is *d inside it wich is QNetworkCookiePrivate and insite that there is a QSharedData (commend,domain,expirationDate,httpOnly,name,path, secure,value) and they are all "not in scope"
wysota
4th May 2009, 23:34
I don't know how to explain what happened...
You tried debugging a class your debugger has no support for. That's normal. Forget the debugger - after making the cast see the size of the list. If it is 0 then it's not a list but rather a QNetworkCookie object which would be strange. Try running this code (assuming "rep" is your QNetworkReply object):
QVariant var = rep->header(QNetworkRequest::SetCookieHeader);
qDebug() << "Cookies set:" << var;
QList<QNetworkCookie> cookies = qvariant_cast<QList<QNetworkCookie> >(var);
foreach(QNetworkCookie c, cookies){
qDebug() << QString(c.toRawForm());
}
cherva
4th May 2009, 23:45
Again it's only one cookie. The JSESSIONID.
wysota
5th May 2009, 08:41
Again it's only one cookie. The JSESSIONID.
Great, finally... Now you should check if setting multiple set-cookie headers is a valid policy with HTTP/1.1. If so then probably reporting a bug to Qt Software is a good idea. If not, then you have to find a workaround for the faulty server for instance using QNetworkReply::rawHeaderList() and QNetworkCookie::parseCookies().
ChrisW67
5th May 2009, 09:47
From RFC 2109, "An origin server may include multiple Set-Cookie headers in a response. Note that an intervening gateway could fold multiple such headers into a single header."
Servers do return multiple Set-Cookie lines, for example (wrapping of line 10 is mine):
Resolving www.doubleclick.com... 216.73.93.8
Connecting to www.doubleclick.com|216.73.93.8|:80... connected.
HTTP request sent, awaiting response...
HTTP/1.1 200 OK
Connection: keep-alive
Date: Tue, 05 May 2009 08:43:11 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Set-Cookie: ecm=user_id=0&isMembershipUser=0&site_id=&username=&new_site=/
&unique_id=0&site_preview=0&langvalue=0&DefaultLanguage=1033&NavLanguage=1033&
LastValidLanguageID=1033&ContType=&UserCulture=1033&SiteLanguage=1033; path=/
Set-Cookie: ASP.NET_SessionId=arv3wz3p3eqaayff0d1duv45; path=/; HttpOnly
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 30533
Length: 30533 (30K) [text/html]
cherva
5th May 2009, 10:48
I sent a bug report to QT. QNetworkReply::rawHeaderList() tells me that there is only one Set-Cookie ..... I think it is affected too. Can I dump the header to a QByteArray and cut the part I don't need ?
wysota
5th May 2009, 21:01
You can dump whatever you want wherever you want if there is an API call for that. Unfortunately I don't see means to do that in the API.
cherva
5th May 2009, 21:03
Anyone having other ideas ?
wysota
5th May 2009, 23:03
Anyone having other ideas ?
You can use QHttp or even QTcpSocket or implement a workaround in Qt itself.
cherva
5th June 2009, 15:42
For anyone having the same issue this problem is fixed in QT 4.5.2 and UP.
Thanks ChrisW67, and wysota.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.