Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt WebKit
  4. QWebView cookies?

QWebView cookies?

Scheduled Pinned Locked Moved Qt WebKit
5 Posts 5 Posters 12.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jano
    wrote on 5 Aug 2011, 14:30 last edited by
    #1

    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]

    1 Reply Last reply
    0
    • P Offline
      P Offline
      ProudChild
      wrote on 27 Oct 2011, 14:33 last edited by
      #2

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

      1 Reply Last reply
      0
      • K Offline
        K Offline
        KA51O
        wrote on 28 Oct 2011, 07:44 last edited by
        #3

        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.

        1 Reply Last reply
        0
        • H Offline
          H Offline
          hungnd
          wrote on 30 Oct 2011, 16:35 last edited by
          #4

          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!

          1 Reply Last reply
          0
          • G Offline
            G Offline
            gillesm
            wrote on 31 Dec 2011, 18:14 last edited by
            #5

            [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]

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved