User-Agent not updating in header_map on the fly

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

User-Agent not updating in header_map on the fly

Postby iamJo » Tue May 03, 2016 4:49 am

Hi

Using - CEF 3.2526.1373.gb660893 (113MB) - Chromium 47.0.2526.80

My project requirement is to have specific userAgent value, which i got to know from this support forum that can be done by overriding OnBeforeResourseLoad method.

My code is mentioned below, but still client is sending the
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.16 Safari/537.36

not appending the "my_client" string at the end.

Am i missing something ? Could someone please help me on this ?

cef_return_value_t Cef3ClientHandler::OnBeforeResourceLoad(
CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefRequest> request,
CefRefPtr<CefRequestCallback> callback) {

CefRequest::HeaderMap header_map;
request->GetHeaderMap(header_map);

CString additionalStr = _T(" my_client");
CefString UserAgentStr = header_map.find("User-Agent")->second;
CString customUserAgent = UserAgentStr.c_str() + additionalStr;

header_map.erase("User-Agent");
header_map.insert(std::make_pair("User-Agent", (CefString)customUserAgent));

request->SetHeaderMap(header_map);

return RV_CONTINUE;
}
iamJo
Newbie
 
Posts: 5
Joined: Tue May 03, 2016 4:21 am

Re: User-Agent not updating in header_map on the fly

Postby fddima » Tue May 03, 2016 10:49 am

I think that it is just setup own headers later.
fddima
Master
 
Posts: 788
Joined: Tue Dec 07, 2010 6:10 am

Re: User-Agent not updating in header_map on the fly

Postby amaitland » Tue May 03, 2016 5:41 pm

Just confirming that you need to be able to dynamically set the User-Agent string?

Otherwise there's apidocs3/projects/%28default%29/_cef_settings_t.html#user_agent
Maintainer of the CefSharp project.
amaitland
Virtuoso
 
Posts: 1292
Joined: Wed Jan 14, 2015 2:35 am

Re: User-Agent not updating in header_map on the fly

Postby iamJo » Wed May 04, 2016 1:08 am

Yes, need to set User-Agent string dynamically.

If i set User-Agent string in CefSettings before CEFInitialize(,,,) like CefString(&settings.user_agent).FromASCII("My_User_Agent_string"); reflecting fine not the default one.
But i want to append "my_client" string in the last of default User-Agent string after CEFInitialize(,,,), expected like :
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.16 Safari/537.36 my_client"

which afaik can be done in OnBeforeResourceLoad() method. Is there any other way to implement this ?

similiar to these posts -
http://www.magpcss.org/ceforum/viewtopi ... =6&t=11716
https://groups.google.com/forum/#!topic ... sZC_2FFsFA
iamJo
Newbie
 
Posts: 5
Joined: Tue May 03, 2016 4:21 am

Re: User-Agent not updating in header_map on the fly

Postby amaitland » Wed May 04, 2016 3:15 am

iamJo wrote:which afaik can be done in OnBeforeResourceLoad() method. Is there any other way to implement this ?


It can be set in OnBeforeResourceLoad. I'm not aware of any other method.

Quick test using `CefSharp` and it appears to be set correctly. Tested successfully with 3.2623.1396 and 3.2526.1362

I added to code to the examples (commented out).

https://github.com/cefsharp/CefSharp/bl ... ler.cs#L69
Maintainer of the CefSharp project.
amaitland
Virtuoso
 
Posts: 1292
Joined: Wed Jan 14, 2015 2:35 am


Return to Support Forum

Who is online

Users browsing this forum: Google [Bot] and 50 guests