When we develop an application based on CEF, there may be a need for URL request processing, such as HTTP download or upload, which can be done using the class library provided by CEF without having to implement or introduce other third-party class libraries yourself.
In CEF, two sets of classes are designed for URL request, one for performing network requests, and one for requesting data.
Foruok original, reproduced please retain the source or follow Foruok's subscription number "program Horizon" to contact Foruok.
URLRequest
Cefurlrequest is the class (interface) that executes the URL request, and the corresponding header file is Cef_urlrequest.h, which is implemented in the libcef/common/urlrequest_impl.cc file.
The static method of the Cefurlrequest class create () creates and executes a URL request. Its prototype is as follows:
static CefRefPtr<CefURLRequest> Create( CefRefPtr<CefRequest> request, CefRefPtr<CefURLRequestClient> client, CefRefPtr<CefRequestContext> request_context);
The first parameter, the type is Cefrequest, represents a URL request, the CEF library has been implemented internally, and is described later.
The second parameter, type cefurlrequestclient, is used to receive the state and data returned by the server, requiring us to inherit the Cefurlrequestclient interface to implement a non-abstract class. There's the back.
The third argument, Cefrequestcontext, is null when the interior creates an appropriate context for itself, not NULL when using the incoming context.
The Create method creates the corresponding URLRequest class based on whether the current browser process or the renderer process, cefbrowserurlrequest (browser_urlrequest_impl.h/. CC) or Cefrenderurlrequest (render_urlrequest_impl.h/.cc).
With this analysis, we're going to make a URL request and actually do the job:
- Construct a cefrequest that represents our request
- Write a class to implement the Cefurlrequestclient interface to handle the response.
- Call Cefurlrequest::create () to create a URL request processing object
Construct request
The Cefrequest class represents a URL request that can be configured with methods, URLs, headers, uploaded data, and so on. The following code snippet demonstrates how to construct a Cefrequest object:
CefRefPtr<CefPostData> data = CefPostData::Create();CefRefPtr<CefPostDataElement> element = CefPostDataElement::Create();const char szData[] = "Hello World!";element->SetToBytes(sizeof(szData) - 1, (const void*)szData);data->AddElement(element);CefRequest::HeaderMap headers;headers.insert(std::make_pair("Content-Type", "text/plain"));headers.insert(std::make_pair("Accept", "text/plain"));CefRefPtr<CefRequest> req = CefRequest::Create();req->SetMethod("POST");req->SetURL("http://xxx.net");req->SetHeaderMap(headers);req->SetPostData(data);
The classes and interfaces associated with a request are in cef_request.h, and are implemented in request_impl.cc. These classes have static create methods that return an interface that represents a specific instance, and then customize the instance object by means of an interface, and the custom object can be used for URL requests.
The code snippet just showed how to construct a Cefrequest object that uses the following class (interface):
- Cefrequest, which represents a URL request
- Cefpostdata, which manages the data to be sent through the request, maintains multiple cefpostdataelement internally, each cefpostdataelement representing a data element to be sent
- Cefpostdataelement, represents the data sent, provides some interfaces that can be associated to a file, or can send bytes directly
To learn about the interfaces of these classes, open the header file and look at it.
Implementing the Cefurlrequestclient Interface
The implementation of the Cefurlrequestclient interface can be very simple, and I have implemented a simple urlrequestclient class.
UrlRequestClient.h as follows:
#ifndef url_request_client_h#define url_request_client_h#include <string> #include "include/cef_urlrequest.h" #include "include/wrapper/cef_helpers.h" class Urlrequestcompletioncallback{public:virtual ~ Urlrequestcompletioncallback () {} virtual void oncompletion (Cefurlrequest::errorcode ErrorCode, const std::string & data) = 0;}; Class Urlrequestclient:public Cefurlrequestclient{public:urlrequestclient (): M_callback (0) {CEF_R Equire_ui_thread (); } urlrequestclient (Urlrequestcompletioncallback *callback): M_callback (callback) {cef_require_ui_th READ (); }////interfaces of cefurlrequestclient//void Onrequestcomplete (cefrefptr<cefurlrequest> request) OVE Rride; void Onuploadprogress (cefrefptr<cefurlrequest> request, int64, int64 total) OVERRIDE; void Ondownloadprogress (cefrefptr<cefurlrequest> request, int64, int64 total) OVERRIDE; voidOndownloaddata (cefrefptr<cefurlrequest> request, const void* data, size_t data_length) OVERRIDE; BOOL Getauthcredentials (bool IsProxy, const cefstring& Host, int port, const cefstring& Realm , const cefstring& Scheme, cefrefptr<cefauthcallback> callback) override{return false; } void Request (cefrefptr<cefrequest> cef_request); void Get (const std::string &url, const cefrequest::headermap &headers = Cefrequest::headermap ()); void Post (const std::string &url, const cefrefptr<cefpostdata> data, const CEFREQUEST::HEADERMAP &headers = Cefrequest::headermap ()); void Setcompletioncallback (Urlrequestcompletioncallback *callback) {m_callback = callback; }private:urlrequestcompletioncallback *m_callback; Cefrefptr<cefurlrequest> m_urlrequest; Std::string m_data; Implement_refcounting (urlrequestclient); Disallow_copy_and_assign (urlrequestClient);}; Class Printurlreqcallback:public urlrequestcompletioncallback{public:void oncompletion (CefURLRequest::ErrorCode ErrorCode, const std::string& data);}; #endif
The
UrlRequestClient.cpp content is as follows:
#include "UrlRequestClient.h" #include <windows.h>void urlrequestclient::onrequestcomplete (cefrefptr< Cefurlrequest> request) {cef_require_ui_thread (); if (m_callback) {m_callback->oncompletion (Request->getrequesterror (), m_data); }}void urlrequestclient::onuploadprogress (cefrefptr<cefurlrequest> request, int64 current, int64 total) {}void Urlrequestclient::ondownloadprogress (cefrefptr<cefurlrequest> request, int64 current, int64 total) {char szlog[ 128] = {0}; sprintf_s (Szlog, $, "urlrequestclient::ondownloadprogress, Current-%lld, total-%lld\r\n", current, total); Outputdebugstringa (Szlog);} void Urlrequestclient::ondownloaddata (cefrefptr<cefurlrequest> request, const void* data, size_t data_length) {m _data + = std::string (static_cast<const char*> (data), data_length);} void Urlrequestclient::request (cefrefptr<cefrequest> cef_request) {m_urlrequest = CefURLRequest::Create (cef_ Request, this, NULL);} void Urlrequestclient::get (const std::string &url, const cefrequest::headermap &headers/*= CefRequest::HeaderMap ( ) */) {cefrefptr<cefrequest> req = cefrequest::create (); Req->seturl (URL); Req->setmethod ("GET"); Req->setheadermap (headers); Request (req);} void Urlrequestclient::P ost (const std::string &url, const cefrefptr<cefpostdata> data, const cefrequest:: Headermap &headers/*= cefrequest::headermap () */) {cefrefptr<cefrequest> req = cefrequest::create (); Req->seturl (URL); Req->setmethod ("POST"); Req->setheadermap (headers); Req->setpostdata (data); Request (req);} For Test//void printurlreqcallback::oncompletion (cefurlrequest::errorcode ErrorCode, const std::string& Data ) {char szlog[128] = {0}; sprintf_s (Szlog, $, "printurlreqcallback::oncompletion, ErrorCode =%d, Data.len =%d, data:\r\n", ErrorCode, Da Ta.length ()); Outputdebugstringa (Szlog); Delete this;}
The
Urlrequestclient class can initiate URL requests and process responses. Its usage is similar to the following (note to use in the UI thread of the browser process):
Get () testurlrequestclient *client = new Urlrequestclient (new printurlreqcallback), std::string url ("http:// Www.baidu.com "); Client->get (URL);/Request () testcefrefptr<cefrequest> req = Cefrequest::create (); req- >setmethod ("GET"); Req->seturl ("http://www.csdn.net"); Cefrequest::headermap Headers;headers.insert (Std::make_pair ("Accept", "Text/html,application/xhtml+xml, application/xml;q=0.9,image/webp,*/*;q=0.8 ") Headers.insert (Std::make_pair (" accept-encoding "," Gzip,deflate, Sdch ") Headers.insert (Std::make_pair (" Accept-language "," En,zh "), Req->setheadermap (headers);(new Urlrequestclient (new Printurlreqcallback))->request (req);//Post () testcefrefptr<cefpostdata> data = Cefpostdata::create (); cefrefptr<cefpostdataelement> element = Cefpostdataelement::create (); const char szdata[] = "Hello world!"; Element->settobytes (sizeof (szdata)-1, (const void*) szdata);d ata->addelement (Element); Cefrequest::headermap Headers;headers.insert (Std::make_pair ("Content-type "," Text/plain ")), Headers.insert (Std::make_pair (" Accept "," Text/plain "));(new Urlrequestclient (new Printurlreqcallback)->post ("Http://xxx.com/hello", data, headers);
That's it.
For a thread in CEF, refer to: http://blog.csdn.net/foruok/article/details/50674141.
For the process, you can refer to: http://blog.csdn.net/foruok/article/details/50621751.
Other reference articles are described in my column: "CEF and Ppapi development ".
Processing HTTP requests using the CEF class library