How to send json-file to the address?
-
-
Can the code be adapted for this purpose:
QUrl url( ui->edURL->text() ); QNetworkRequest request( url ); QString auth = QString( "%1:%2" ).arg( "api" ).arg( ui->edKey->text() ); request.setRawHeader( "Authorization", "Basic " + auth.toLatin1().toBase64() ); request.setHeader( QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded" ); QUrl params; params.addQueryItem( "from", ui->edFrom->text() ); params.addQueryItem( "to", ui->edTo->text() ); params.addQueryItem( "subject", ui->edSubject->text() ); params.addQueryItem( "text", ui->txtMessage->toPlainText() ); m_manager->post( request, params.encodedQuery() );
?
-
There are examples on how to use websockets with Qt: https://doc.qt.io/qt-5/qtwebsockets-examples.html
-
I am trying to connect to the socket so but qdebug return false. What do I need to fix?
.hQWebSocket myWebSocket; QUrl myUrl;
.cpp
myUrl = "wss://ws.binaryws.com/websockets/v3?app_id=xlY2chJR33XoSL5"; myWebSocket.open(QUrl(myUrl)); qDebug()<<myWebSocket.isValid();
-
@Mikeeeeee said in How to send json-file to the address?:
What do I need to fix?
Read the documentation and examples and see that all socket functions are async in Qt.
-
I tried to use the class from the example "SSL Echo Client Example", but it also does not work. Please tell me how to connect to the websocket?
cppSslEchoClient client(QUrl(QStringLiteral("wss://ws.binaryws.com/websockets/v3?app_id=xlY2chJRk0XoSL5"))); qDebug()<<client->m_webSocket.isValid()<<client->m_webSocket.state();
debug return: false QAbstractSocket::UnconnectedState
-
Again: how should it work when the socket has no time to do the actual connection? The operations are asynchronous !
-
@Mikeeeeee said in How to send json-file to the address?:
Okay, can you please tell me how I can run it asynchronously?
You already do. You need to connect a slot to https://doc.qt.io/qt-5/qwebsocket.html#connected signal and in that slot call
qDebug()<<myWebSocket.isValid();
"Or maybe the program can wait until a socket connection appears?" - why don't you simply use signals/slots?!
-
I took the example code, and it says so:
SslEchoClient::SslEchoClient(const QUrl &url, QObject *parent) : QObject(parent) { connect(&m_webSocket, &QWebSocket::connected, this, &SslEchoClient::onConnected); connect(&m_webSocket, QOverload<const QList<QSslError>&>::of(&QWebSocket::sslErrors), this, &SslEchoClient::onSslErrors); m_webSocket.open(QUrl(url)); }
There are signals and slots, I create an object of this class, but it does not connect. Why?
-
@Mikeeeeee said in How to send json-file to the address?:
but it does not connect. Why?
You know, there is https://doc.qt.io/qt-5/qwebsocket.html#error-1 signal.
Did you connect a slot to it to see whether you get an error? -
@Mikeeeeee said in How to send json-file to the address?:
QWebSocket::sslErrors
why the overload ? as far as I can see
sslErrors
has no alternatives to QList<QSslErrors> -
Tried to start an example "SslEchoClient", but it irzhe does not connect and does not display error messages.
In my project I connected the status output to the button:#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); myUrl = "wss://ws.binaryws.com/websockets/v3?app_id=xlY2chJRk0XoSL5"; myWebSocket.open(QUrl(myUrl)); qDebug()<<myWebSocket.isValid()<<myWebSocket.state(); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_testButton_clicked() { qDebug()<<myWebSocket.isValid()<<myWebSocket.state()<<myWebSocket.error(); }
Debug return:
false QAbstractSocket::UnconnectedState QAbstractSocket::UnknownSocketError
false QAbstractSocket::UnconnectedState QAbstractSocket::UnknownSocketError
false QAbstractSocket::UnconnectedState QAbstractSocket::UnknownSocketError
false QAbstractSocket::UnconnectedState QAbstractSocket::UnknownSocketErrorWhy can't I even use the example "SslEchoClient" to connect to the websocket?
-
@Mikeeeeee DO YOU HAVE ANY ERRORS WHEN TRYING TO CONNECT? After calling myWebSocket.open(QUrl(myUrl).
Did you connect a slot to https://doc.qt.io/qt-5/qwebsocket.html#error-1 as I suggested?
Is your onSslErrors called? -
I did, but debag doesn't say it was a error:
myUrl = "wss://ws.binaryws.com/websockets/v3?app_id=xlY2chJRk0XoSL5"; myWebSocket.open(QUrl(myUrl)); //myWebSocket qDebug()<<myWebSocket.isValid()<<myWebSocket.state(); connect(&myWebSocket, QOverload<QAbstractSocket::SocketError>::of(&QWebSocket::error), [=](QAbstractSocket::SocketError error){ qDebug()<<"i have error";});
Example "SslEchoClient" also no message, means no error, but the websocket is not connected.
-
@Mikeeeeee If you use Google to search for "http 301" you will find https://en.wikipedia.org/wiki/HTTP_301 which tells you that 301 status code means "Moved Permanently is used for permanent URL redirection". What URL are you using?