QWebView cookies?
-
Hi,
I want to show some web page on QWebView. But this page require authentication.
Authentication token ( and some other properties ) should be passed via standard http cookie.
How can i do that? Any suggestion, code examples?
I tried follwing:
@
QUrl url("https://blabla.com");
QList<QNetworkCookie> cookies;
QNetworkCookie cookie(QByteArray("token"),QByteArray("1234"));
cookies.append(cookie);
MyCookieJar*cookieJar = new MyCookieJar(this);
cookieJar->setCookies(cookies);
_webView->page()->networkAccessManager()->setCookieJar(cookieJar);
_webView->load( url );
@Note:
MyCookieJar just extedn QNetworkCookieJar and override protected function void setAllCookies ( const QList<QNetworkCookie> & cookieList )Any idea?
Many Thanks for any help!
[EDIT: fixed code syntax typo, Volker]
-
The response to this problem would help me, any ideas?
its a shame all the cool questions about webkit get unanswered -
[quote author="hungnd" date="1319992506"]I think u can handle the event loadStarted of QWebView widget, then use webView->page()->mainFrame()->evaluateJavaScript to execute javascript to set cookies as u want. Good luck![/quote]
This is not an answer
I had the same problem you describe.You have to construct the cookie and not consider all the lines as one line...
You have to do like this :
@
QList<QNetworkCookie> cookies;
QStringList list=U.GetValueConfig("cookie").split("\n");
// I store the cokkies in base all the lines separated ny \n
foreach ( QString elem, list)
{
QNetworkCookie cookie; /// create a cookie
cookie.setName(C.Name(elem));
cookie.setValue(C.Value(elem));
cookie.setDomain(C.Domain(elem));
cookie.setExpirationDate(C.Expirationdate(elem));
cookie.setPath(C.Path(elem));
cookie.setHttpOnly(C.HttpOnly(elem));
cookies.append(cookie);
}
MonCookieJar *jar = new MonCookieJar;
jar->SetAllCookies(cookies);
@C.name is a function reading a cookie line and extracting the value
@
QByteArray cookie::Name ( QString str)
{
return (str.split(QRegExp("; *")).at(0).split("=").at(0).toAscii());
}
@You have to write Domain Value
For the ExpiratioDate you have to make a QDateTime fonction
be care QDateTime::fromString will work in your locale for me in france Lundi, dimanche etc.. you need to convert@
QDateTime cookie::Expirationdate (QString str)
{
int index=str.split(QRegExp("; ")).indexOf(QRegExp("^expires=."));
QString date=str.split(QRegExp("; *")).at(index).split("=").at(1);
date.replace("Jan",QDate::longMonthName(1));
date.replace("Feb",QDate::longMonthName(2));
date.replace("Mar",QDate::longMonthName(3));
date.replace("Apr",QDate::longMonthName(4));
date.replace("May",QDate::longMonthName(5));
date.replace("Jun",QDate::longMonthName(6));
date.replace("Jul",QDate::longMonthName(7));
date.replace("Aug",QDate::longMonthName(8));
date.replace("Sep",QDate::longMonthName(9));
date.replace("Oct",QDate::longMonthName(10));
date.replace("Nov",QDate::longMonthName(11));
date.replace("Dec",QDate::longMonthName(12));
date.replace("Mon",QDate::longDayName(1));
date.replace("Tue",QDate::longDayName(2));
date.replace("Wed",QDate::longDayName(3));
date.replace("Thu",QDate::longDayName(4));
date.replace("Fri",QDate::longDayName(5));
date.replace("Sat",QDate::longDayName(6));
date.replace("Sun",QDate::longDayName(7));
date.replace(" GMT","");
QDateTime Date= QDateTime::fromString(date,"dddd, dd-MMMM-yyyy hh:mm:ss");
return (Date);
}
@[EDIT: fixed quote formatting and code formatting, please use @-tags for code and the quote linke for quoting comments, Volker]