Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
Posts: 93
Registered: ‎08-18-2011
My Device: 9550
My Carrier: At&T
Accepted Solution

How to Set cookie in Cascade WebView?

Hi,

 

I want to create webview component using QML and I need to launch multiple applications in multiple webview and each webview have different set of cookies.

 

Now my question is :

 

How to set cookies in each webview?

 

I googled it but not getting any example or clue. Please help me to go ahead. Any example would be appreciated.

 

 

Thanks,

Shwarup Biswas
Developer
Posts: 526
Registered: ‎05-17-2009
My Device: 9900
My Carrier: ATT

Re: How to Set cookie in Cascade WebView?

[ Edited ]

Check out WebCookieJar in the webview documentation

 

https://developer.blackberry.com/cascades/reference/bb__cascades__webcookiejar.html

Like all of my posts
Developer
Posts: 93
Registered: ‎08-18-2011
My Device: 9550
My Carrier: At&T

Re: How to Set cookie in Cascade WebView?

Hi Kylefowler,

 

I really appreciate your help but I am a beginner in C++, QML language. I can't figure it out how to implement it. Please give me an example if possible.

 

 

Thanks,

Shwarup Biswas
Developer
Posts: 93
Registered: ‎08-18-2011
My Device: 9550
My Carrier: At&T

Re: How to Set cookie in Cascade WebView?

hi Guys,

this is what i am doing please guide me what is wrong here.

 

I am sent http post request and received a complete HTML page in response. then i displayed this response HTML page in Cascade WebView.


In this response header i have cookie that I need to send in the following http request.

So, after getting complete HTML page in response, I am trying to save cookie value in WebCookieJar.

I created an object to access the WebCookieJar and WebStorage object in PostHttp.hpp file as bellow:
    bb::cascades::WebCookieJar* m_webCookieJar;
    bb::cascades::WebStorage* m_webStorage;
    
Then I want to instentiage WebCookieJar in PostHttp.cpp file as bellow:
    m_webCookieJar = m_webStorage->cookieJar();

    
At this point my appliction closed automatically and getting bellow error.
    Process 268869854 (VZWSecurity) terminated SIGSEGV code=1 fltno=11 ip=78eaf708(/base/usr/lib/libbbcascades.so.1@_ZN2bb8cascades10WebStorageC1ERNS0_17WebStoragePrivateE+0x27) mapaddr=001af708. ref=00000008
    
    please help me to find where i am doing mistake?

Thanks,

Shwarup Biswas
Highlighted
Developer
Posts: 526
Registered: ‎05-17-2009
My Device: 9900
My Carrier: ATT

Re: How to Set cookie in Cascade WebView?

you should grab the settings object from the webview.

WebCookieJar* jar = webview->storage()->cookieJar()

jar->setCookiesForUrl("my url", QStringList())
Like all of my posts
Developer
Posts: 93
Registered: ‎08-18-2011
My Device: 9550
My Carrier: At&T

Re: How to Set cookie in Cascade WebView?

Hi Kylefowler,

 

Could you please give a line how to "grab the setting object from the webview"?

 

Thanks for your co-operation.

 

Thanks,

Shwarup Biswas
Developer
Posts: 93
Registered: ‎08-18-2011
My Device: 9550
My Carrier: At&T

Re: How to Set cookie in Cascade WebView?

Hi,

 

I post my code bellow. the highlighted code is used to set cookie value in WebCookieJar.

 

 

 

postHttp.hpp
------------
#ifndef POSTHTTP_HPP
#define POSTHTTP_HPP
#include <bb/cascades/WebCookieJar>
#include <bb/cascades/WebStorage>
#include <bb/cascades/WebSettings>
#include <bb/cascades/WebView>

#include <QtCore/QObject>

class QNetworkAccessManager;

class PostHttp : public QObject
{
    Q_OBJECT
public:
    PostHttp(QObject* parent = 0);

public Q_SLOTS:
    void post(const QString &body);

Q_SIGNALS:
    void complete(const QString &info);

private Q_SLOTS:
    void onGetReply();

    
private:
    QNetworkAccessManager* m_networkAccessManager;

    // The central object to access the WebCookieJar
    bb::cascades::WebView* m_webView;
    
    // The central object to access the WebCookieJar
    bb::cascades::WebCookieJar* m_webCookieJar;
};

#endif


postHttp.cpp
---------------

#include "PostHttp.hpp"
#include <QDebug>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QSslConfiguration>
#include <QUrl>
#include <QNetworkAccessManager>

using namespace bb::cascades;

PostHttp:Smiley TongueostHttp(QObject* parent)
    : QObject(parent)
    , m_networkAccessManager(new QNetworkAccessManager(this))

{
}

void PostHttp:Smiley Tongueost(const QString &body)
{
    QNetworkRequest request;
    request.setHeader(QNetworkRequest::ContentTypeHeader, "text/html");

        request.setUrl(QUrl("https://xyz.com/device/login.htm"));

        QSslConfiguration config = request.sslConfiguration();

        config.setPeerVerifyMode(QSslSocket::VerifyNone);

        config.setProtocol(QSsl::TlsV1);

        request.setSslConfiguration(config);

    QNetworkReply* reply = m_networkAccessManager->post(request, body.toAscii());

    connect(reply, SIGNAL(finished()), this, SLOT(onGetReply()));


    m_networkAccessManager->setCookieJar(new QNetworkCookieJar(m_networkAccessManager));


}

void PostHttp:Smiley SurprisednGetReply()
{
    QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());

    QList<QByteArray> headers=reply->rawHeaderList();
    QVariant cookies=reply->header(QNetworkRequest:Smiley FrustratedetCookieHeader);

    QList<QVariant> list = cookies.toList();

    QString str =""+reply->rawHeader("Set-Cookie");

    m_webCookieJar = m_webView->storage()->cookieJar();
    
    QString response;
    if (reply) {
        if (reply->error() == QNetworkReply::NoError) {
            const int available = reply->bytesAvailable();
            if (available > 0) {
                const QByteArray buffer(reply->readAll());
                response = QString::fromUtf8(buffer);
            }
        } else {
            response = tr("Error..: %1 status: %2").arg(reply->errorString(), reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toString());
        }

        reply->deleteLater();
    }

    if (response.trimmed().isEmpty()) {
        response = tr("Unable to retrieve post response");
    }
   emit complete(response);
}

 

In httpPost.cpp i am trying to set cookie in WebCookieJar as bellow:

    m_webCookieJar = m_webView->storage()->cookieJar();

 

But run time application close abruptly with bellow error message:

Process 283152605 (VZWSecurity) terminated SIGSEGV code=1 fltno=11 ip=78d05ab4(/base/usr/lib/libbbcascades.so.1@_ZN2bb8cascades7WebViewC2EPNS0_9ContainerE+0x20f) mapaddr=00105ab4. ref=00000010

 

Please help me to achieve it.

 

 

Thanks,

Shwarup Biswas
Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: How to Set cookie in Cascade WebView?

From this code it seems that m_webView is declared but is never initialized, that's why the program crashes.

WebView* is just a memory address. By default it contains some random (undefined) address.

 

Pass a pointer to the actual webView instance to the constructor and initialize m_webView there:

 

PostHttp:PostHttp(WebView *webView, QObject* parent)
: QObject(parent)
, m_networkAccessManager(new QNetworkAccessManager(this))
, m_webView(webView)
{
}
 

webCookieJar can be declared as a local variable if you don't need to use it in other functions:

 

 

WebCookieJar *webCookieJar = m_webView->storage()->cookieJar();

 


Andrey Fidrya, @zmeyc on twitter
Developer
Posts: 93
Registered: ‎08-18-2011
My Device: 9550
My Carrier: At&T

Re: How to Set cookie in Cascade WebView?

Hi Zmey,

 

I really appreciate your help. This is my first Cascade application. So sorry for all basic questions. Could you please check my "MyConnectionClass.cpp" code bellow and guide me how can I call this constractor "PostHttpSmiley TongueostHttp(WebView *webView, QObject* parent)" .

 

The highlited red line is used to call this PostHttp class.

 

 

// Navigation pane project template

#include <bb/cascades/Application>

#include <bb/cascades/QmlDocument>

#include <bb/cascades/AbstractPane>

#include <QSettings>

#include "MyConnectionClass.hpp"

#include "PostHttp.hpp"

using namespace bb::cascades;

 

MyConnectionClass::MyConnectionClass(bb::cascades::Application *app): QObject(app)

{

qmlRegisterType<PostHttp>("Network.PostHttp", 1, 0, "PostHttp");

 

//add a QTimer class as a qml type

qmlRegisterType<QTimer>("my.library", 1, 0, "QTimer");

 

// create scene document from main.qml asset// set parent to created document to ensure it exists for the whole application lifetime

QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);

qml->setContextProperty("securityApp", this);

 

// create root object for the UI

AbstractPane *root = qml->createRootObject<AbstractPane>();

// set created root object as a scene

app->setScene(root);

}

Thanks,

Shwarup Biswas
Developer
Posts: 93
Registered: ‎08-18-2011
My Device: 9550
My Carrier: At&T

Re: How to Set cookie in Cascade WebView?

Hi,

 

Could you give me an example how  to obtain an instance of Cascade WebView class?

 

Thanks,

Shwarup Biswas