August 5, 2011

Jano Jano
Lab Rat
19 posts

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:

  1. QUrl url("https://blabla.com");
  2.     QList<QNetworkCookie> cookies;
  3.     QNetworkCookie cookie(QByteArray("token"),QByteArray("1234"));
  4.     cookies.append(cookie);
  5.     MyCookieJar*cookieJar = new MyCookieJar(this);
  6.     cookieJar->setCookies(cookies);
  7.     _webView->page()->networkAccessManager()->setCookieJar(cookieJar);
  8.     _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]

4 replies

October 27, 2011

ProudChild ProudChild
Lab Rat
9 posts

The response to this problem would help me, any ideas?
its a shame all the cool questions about webkit get unanswered

October 28, 2011

KA51O KA51O
Robot Herder
478 posts

I feel the same way. Maybe this just isn’t the right forum and we need to ask them in the a WebKit forum, although I must admit I haven’t found one yet.

October 30, 2011

hungnd hungnd
Lab Rat
13 posts

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!

December 31, 2011

gillesm gillesm
Lab Rat
1 posts
hungnd wrote:
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!

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 :

  1. QStringList list=U.GetValueConfig("cookie").split("\n");
  2. // I store the cokkies in base all the lines separated ny \n
  3.    foreach ( QString elem, list)
  4.    {
  5.       QNetworkCookie cookie; /// create a cookie
  6.       cookie.setName(C.Name(elem));
  7.       cookie.setValue(C.Value(elem));
  8.       cookie.setDomain(C.Domain(elem));
  9.       cookie.setExpirationDate(C.Expirationdate(elem));
  10.       cookie.setPath(C.Path(elem));
  11.       cookie.setHttpOnly(C.HttpOnly(elem));
  12.       cookies.append(cookie);
  13.    }
  14.    MonCookieJar *jar = new MonCookieJar;
  15.    jar->SetAllCookies(cookies);

C.name is a function reading a cookie line and extracting the value

  1. QByteArray cookie::Name ( QString str)
  2. {
  3.    return (str.split(QRegExp("; *")).at(0).split("=").at(0).toAscii());
  4. }

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

  1. QDateTime cookie::Expirationdate (QString str)
  2. {
  3.    int index=str.split(QRegExp("; *")).indexOf(QRegExp("^expires=.*"));
  4.    QString date=str.split(QRegExp("; *")).at(index).split("=").at(1);
  5.    date.replace("Jan",QDate::longMonthName(1));
  6.    date.replace("Feb",QDate::longMonthName(2));
  7.    date.replace("Mar",QDate::longMonthName(3));
  8.    date.replace("Apr",QDate::longMonthName(4));
  9.    date.replace("May",QDate::longMonthName(5));
  10.    date.replace("Jun",QDate::longMonthName(6));
  11.    date.replace("Jul",QDate::longMonthName(7));
  12.    date.replace("Aug",QDate::longMonthName(8));
  13.    date.replace("Sep",QDate::longMonthName(9));
  14.    date.replace("Oct",QDate::longMonthName(10));
  15.    date.replace("Nov",QDate::longMonthName(11));
  16.    date.replace("Dec",QDate::longMonthName(12));
  17.    date.replace("Mon",QDate::longDayName(1));
  18.    date.replace("Tue",QDate::longDayName(2));
  19.    date.replace("Wed",QDate::longDayName(3));
  20.    date.replace("Thu",QDate::longDayName(4));
  21.    date.replace("Fri",QDate::longDayName(5));
  22.    date.replace("Sat",QDate::longDayName(6));
  23.    date.replace("Sun",QDate::longDayName(7));
  24.    date.replace(" GMT","");
  25.    QDateTime Date= QDateTime::fromString(date,"dddd, dd-MMMM-yyyy hh:mm:ss");
  26.    return (Date);
  27. }

[EDIT: fixed quote formatting and code formatting, please use @-tags for code and the quote linke for quoting comments, Volker]

 
  ‹‹ QtWebKit 1.0 QML WebView      Mobile web browser ››

You must log in to post a reply. Not a member yet? Register here!