When we develop an application based on CEF, there may be a need for URL request processing, such as HTTP download or upload. It is now possible to use 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 running network requests, and one for requesting data.
Foruok original. Reprint please keep the source or follow Foruok's subscription number "program Horizon" to contact Foruok.
URLRequest
Cefurlrequest is the class (interface) that runs the URL request, the corresponding header file is cef_urlrequest.h, and the implementation is in the libcef/common/urlrequest_impl.cc file.
The static method of the Cefurlrequest class create () creates and runs a URL request. Its prototypes are for example the following:
static CefRefPtr<CefURLRequest> Create( CefRefPtr<CefRequest> request, CefRefPtr<CefURLRequestClient> client, CefRefPtr<CefRequestContext> request_context);
The first parameter, type Cefrequest, represents a URL request. The CEF Library has been implemented internally and will be covered later.
The second number of parameters. The type is cefurlrequestclient, which receives the state and data returned by the server, and requires us to inherit the Cefurlrequestclient interface to implement a non-abstract class.
There's the back.
The third number of parameters. Cefrequestcontext, NULL when the interior creates a suitable context for itself. When not null, the incoming context is used.
The Create method creates the corresponding URLRequest class based on whether the browser process or the renderer process is currently in progress, 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. It can be configured with methods, URLs, headers, uploaded data, etc. The following code snippet shows 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 now request_impl.cc.
These classes have static create methods. The ability to return an interface that represents a detailed instance. Then you can customize the instance object by means of the interface, and the customized object can be used for the URL request.
The code snippet just now shows how to construct a Cefrequest object. When the following classes (interfaces) are used:
- Cefrequest, which represents a URL request
- Cefpostdata, manage the data to be sent through the request. It maintains multiple cefpostdataelement internally. Each cefpostdataelement represents a data element to be sent
- Cefpostdataelement, represents the data sent. Provides a number of interfaces. Can be associated to a file, and 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 easy. I implemented a simple urlrequestclient class.
UrlRequestClient.h such as the following:
#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
UrlRequestClient.cpp content such as the following:
#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. It is used similar to the following (note to be used 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 the thread in CEF, you can refer to: http://blog.csdn.net/foruok/article/details/50674141.
About the process, can participate in: 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