| BOOL SetUserAgent( |
| |
LPCTSTR lpszUserAgent |
|
| ); |
The SetUserAgent method changes the default User-Agent string for this instance.
Parameters
- lpszUserAgent
- A null-terminated string which specifies the new User-Agent
header value for subsequent requests. If this parameter is NULL or
an empty string, the User-Agent header will revert back to the
default value used by the Edge WebView2 control.
Return Value
If the method succeeds, the return value is non-zero. If the method
fails, the return value is zero. To get extended error information, call
GetLastError.
Remarks
The SetUserAgent method sets the string that will be sent
in the User-Agent header for all subsequent HTTP requests made by this browser
instance. This method should always be used to modify the User-Agent header
value; you should not set the header directly using the SetHeader
function.
Applications may want to override the default User-Agent in several
scenarios:
- Requesting a mobile or tablet version of a site by providing a User-Agent string that identifies as a mobile browser.
- Emulating a different browser for testing purposes.
- Including custom product tokens identifying the host application to the server.
The general format of a User-Agent string follows the conventions described
in RFC 9110:
User-Agent: product / version (comment) [ product / version ... ]
For example, a typical User-Agent string might look like:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36 Edg/140.0.0.0
This value is global to the instance. Once changed, it will remain in
effect until explicitly reset or until the browser window is destroyed.
When setting a custom User-Agent value, developers should ensure it is a
syntactically valid string. Some websites may reject requests or provide
degraded functionality if the value is malformed or omits key tokens.
If you want to change the User-Agent string to identify your
application, it is recommended that you append your application name
and version to the default value.
Example
INT nLength = pWebView->GetUserAgent(NULL, 0);
if (nLength <= 0)
{
_tprintf(_T("Unable to get User-Agent string (error 0x%lx)\n"), pWebView->GetLastError());
return;
}
const TCHAR szAppId[] = _T(" MyApp/1.0");
INT nTotalLength = nLength + (INT)_tcslen(szAppId) + 1;
LPTSTR lpszUserAgent = (LPTSTR)malloc(nTotalLength * sizeof(TCHAR));
if (!lpszUserAgent)
return;
if (pWebView->GetUserAgent(lpszUserAgent, nTotalLength) <= 0)
{
_tprintf(_T("Failed to copy User-Agent string (error 0x%lx)\n"), pWebView->GetLastError());
free(lpszUserAgent);
return;
}
_tcscat_s(lpszUserAgent, nTotalLength, szAppId);
if (!pWebView->SetUserAgent(lpszUserAgent))
{
_tprintf(_T("Failed to update the User-Agent string (error 0x%lx)\n"), pWebView->GetLastError());
}
else
{
_tprintf(_T("User-Agent successfully updated to:\n%s\n"), lpszUserAgent);
}
free(lpszUserAgent);
Requirements
Minimum Desktop Platform: Windows 7 Service Pack 1
Minimum Server Platform: Windows Server 2008 R2 Service Pack 1
Header File: cstools12.h
Import Library: cshtmv12.lib
Unicode: Implemented as Unicode and ANSI versions
See Also
|