diff --git a/Release/include/cpprest/ws_client.h b/Release/include/cpprest/ws_client.h index 9c9e5d818d..9a324cde51 100644 --- a/Release/include/cpprest/ws_client.h +++ b/Release/include/cpprest/ws_client.h @@ -150,6 +150,12 @@ class websocket_client_config return m_sni_hostname; } + /// + /// Sets the User Agent to be used for the connection + /// + /// The User Agent to use, as a string. + _ASYNCRTIMP void set_user_agent(const utf8string &user_agent); + /// /// Gets the headers of the HTTP request message used in the WebSocket protocol handshake. /// diff --git a/Release/src/websockets/client/ws_client_wspp.cpp b/Release/src/websockets/client/ws_client_wspp.cpp index e9e7658b18..7c3f2043bc 100644 --- a/Release/src/websockets/client/ws_client_wspp.cpp +++ b/Release/src/websockets/client/ws_client_wspp.cpp @@ -312,6 +312,15 @@ class wspp_callback_client : public websocket_client_callback_impl, public std:: shutdown_wspp_impl(con_hdl, false); }); + // Set User Agent specified by the user. This needs to happen before any connection is created + const auto& headers = m_config.headers(); + + auto user_agent_it = headers.find(web::http::header_names::user_agent); + if (user_agent_it != headers.end()) + { + client.set_user_agent(utility::conversions::to_utf8string(user_agent_it->second)); + } + // Get the connection handle to save for later, have to create temporary // because type erasure occurs with connection_hdl. websocketpp::lib::error_code ec; @@ -323,7 +332,6 @@ class wspp_callback_client : public websocket_client_callback_impl, public std:: } // Add any request headers specified by the user. - const auto & headers = m_config.headers(); for (const auto & header : headers) { if (!utility::details::str_icmp(header.first, g_subProtocolHeader)) diff --git a/Release/src/websockets/client/ws_msg.cpp b/Release/src/websockets/client/ws_msg.cpp index 10bce338e6..1d24409c67 100644 --- a/Release/src/websockets/client/ws_msg.cpp +++ b/Release/src/websockets/client/ws_msg.cpp @@ -28,6 +28,12 @@ namespace client { static ::utility::string_t g_subProtocolHeader = _XPLATSTR("Sec-WebSocket-Protocol"); + +void websocket_client_config::set_user_agent(const utf8string &user_agent) +{ + headers().add(web::http::header_names::user_agent, utility::conversions::to_string_t(user_agent)); +} + void websocket_client_config::add_subprotocol(const ::utility::string_t &name) { m_headers.add(g_subProtocolHeader, name);