Thank you for your feedback. I had worked out that if I replaced
Code: Select all
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
with
I could build the sample HTTP GET request application. I have now replaced this with
Code: Select all
#include <QAccess_Manager>
#include <QNetwork_Reply>
#include <QNetwork_Request>
as you suggested.
In terms of naming conventions, I think all that is required is to add the header include information to the documentation (unless I have missed it somewhere).
However, when executing the CS version of my Qt GET request application with a simple city wttr weather request in my case, I got the error message
“Unknown network protocol: https”
from my QNetworkReply *reply slot which uses the code shown below
Code: Select all
void MainWindow::managerFinished(QNetworkReply *reply)
{
if (reply->error() != QNetworkReply::NoError ) {
//qDebug() << reply->errorString();
qWarning() <<"ErrorNo: "<< reply->error() << "for url: " << reply->url().toString();
qDebug() << "Request failed, " << reply->errorString();
qDebug() << "Headers:"<< reply->rawHeaderList()<< "content:" << reply->readAll();
return;
}
qDebug()<<"Url: "<<reply->url().toString();
QString answer = reply->readAll();
qDebug()<<"Weather ="<<answer;
QMessageBox::information(this, "Wttr Weather Service",
answer);
}
Consequently no data was returned which was not the case with the Qt application. In my Qt code I used a QString to make a https request as shown below.
Code: Select all
void MainWindow::requestWeatherFor(QString city)
{
QString wttrStr="https://wttr.in/"+city+"?format=%l:+%C+%t";
request.setUrl(wttrStr);
manager->get(request);
}
For CS, the wttrStr was changed to an URL.
I tried adding the code line
Code: Select all
request.setSslConfiguration(QSslConfiguration::defaultConfiguration());
only to find I got errors including
class QNetworkRequest’ has no member named ‘setSslConfiguration
This surprised me as the CS API documentation for QNetworkRequest states that there should be a public method called setSslConfiguration.
After some experimenting I changed my get request to
Code: Select all
void MainWindow::requestWeatherFor(QString city)
{
QUrl url;
url.setScheme("http");
url.setHost("wttr.in");
url.setPath("/"+city);
QString key = "format";
QString value=QString::number(4);
QUrlQuery qurlqr;
qurlqr.addQueryItem(key, value);
QString result = qurlqr.toString(QUrl::FullyEncoded);
qDebug()<<"url parameters: "<<result;
url.setQuery(result);
request.setUrl(url);
manager->get(request);
}
The setScheme uses http (not https). In this code I am sending a GET parameter (format = 4) through the requested URL. Weather data was returned with no errors. It is similar to using curl as shown below
curl http://wttr.in/London?format=4
I thought that this information may be helpful for anyone like me trying to convert their Qt QNetworkAccessManager code to CS. I have done some POST tests using the postman-echo website which just returns a JSON blob of what was sent to it. A working code example is below.
Code: Select all
QUrl url("http://postman-echo.com/post");
QByteArray data;
data.append("key1=14");
url.setQuery(data);
request.setRawHeader("Content-Type", "text/plain");
request.setUrl(url);
manager->post(request,data);
However when I try to use https://postman-echo.com/post" I get errors again. Have I overlooked something obvious regarding the use of https like an include path? I have openssl installed and my Qt5 examples do recognize the https url paths correctly.
I have tested OpenWeatherMap as an alternative to wttr using the http protocol and a URL parameter list. I successfully parsed the JSON reply using QJsonDocument to obtain data about temperature, pressure, humidity etc for a specific location. However, again I was not able to use https.
Again thank you for your reply regarding include headers. Some feedback on the above issues would be appreciated.