赞助

CEF 访问需要认证网页或接口(在Request的Headers中添加认证Token)

转载:https://blog.csdn.net/wdw984/article/details/85275289

1.首先要让我们自己的CefClient这个类公有继承CefRequestHandler

2.添加Request事件构造函数

CefRefPtr<CefRequestHandler> GetRequestHandler() OVERRIDE{ return this; }

3.然后重写父类的OnBeforeBrowse和OnBeforeResourceLoad两个方法

在.h文件中添加函数声名

virtual bool OnBeforeBrowse(CefRefPtr<CefBrowser> browser,
            CefRefPtr<CefFrame> frame,
            CefRefPtr<CefRequest> request,
            bool user_gesture,
            bool is_redirect) OVERRIDE;

        virtual ReturnValue OnBeforeResourceLoad(
            CefRefPtr<CefBrowser> browser,
            CefRefPtr<CefFrame> frame,
            CefRefPtr<CefRequest> request,
            CefRefPtr<CefRequestCallback> callback) OVERRIDE;

在.cpp中重写函数

bool CCefBrowserEventHandler::OnBeforeBrowse(CefRefPtr<CefBrowser> browser,
        CefRefPtr<CefFrame> frame,
        CefRefPtr<CefRequest> request,
        bool user_gesture,
        bool is_redirect) {
        CEF_REQUIRE_UI_THREAD();

        return false;
    }

    CefRequestHandler::ReturnValue CCefBrowserEventHandler::OnBeforeResourceLoad(
        CefRefPtr<CefBrowser> browser,
        CefRefPtr<CefFrame> frame,
        CefRefPtr<CefRequest> request,
        CefRefPtr<CefRequestCallback> callback)
    {
        wstring g_tokenValue;
        CefRequest::HeaderMap headers;
        headers.insert(std::make_pair(L"Authorization",g_tokenValue.c_str()));

        request->SetHeaderMap(headers);

        return RV_CONTINUE;
    }

 

posted @ 2019-05-15 14:24  车臣  阅读(2808)  评论(0编辑  收藏  举报