Unable to Open QWebsocket with Username and Password
-
Hello there,
I am currently trying to open a QWebSocket to a kdb+ process which is protected by a username and password. In order to do this from kdb+ and JavaScript, I can just provide the websocket URL as
ws://username:password@host:port
and the connection works as expected.I am trying to test this using the simple Qt example 'echoclient' here. I am using Qt 5.15.2. If I try to use the equivalent
QWebSocket(QUrl(QStringLiteral("ws://username:password@host:port"))
the connection simply doesn't work. The kdb+ server I am trying to connect to doesn't even register the attempted connection.Is it known if it is possible to use QWebSockets to create a WebSocket to a password protected process? Or is there a correct way to authenticate the user and password over HTTP prior to opening the WebSocket perhaps?
Thank you
-
@odriscog said in Unable to Open QWebsocket with Username and Password:
ws://username:password@host:port
You should at least make sure that QUrl() can parse your url
qDebug() << QUrl("ws://username:password@host:port")
But I would guess you simply did not read the documentation of the QWebSocket ctor - it does not take an url as first argument
-
My apologies that's a typo in my initial question. What I meant to say is that I am using
EchoClient client(QUrl(QStringLiteral("ws://user:password@host:port")), debug);
in the linked example which has the constructor
EchoClient::EchoClient(const QUrl &url, bool debug, QObject *parent) : QObject(parent), m_url(url), m_debug(debug) { if (m_debug) qDebug() << "WebSocket server:" << url; connect(&m_webSocket, &QWebSocket::connected, this, &EchoClient::onConnected); connect(&m_webSocket, &QWebSocket::disconnected, this, &EchoClient::closed); connect(&m_webSocket, &QWebSocket::stateChanged, this, [](QAbstractSocket::SocketState state) { qDebug() << state ; } ); m_webSocket.open(QUrl(url)); }
I have switched on debug in the option and the output suggests that the URL is being parsed okay, I get the following output...
WebSocket server: QUrl("ws://user@host") QAbstractSocket::ConnectingState QAbstractSocket::UnconnectedState
-
QWebSocket and QUrl have so many functions to check if they're valid or not or what's currently going on - you should use them.