-
新建项目
T12HttpClient
|
|
|
#include <QCoreApplication>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
int main(int argc,char* argv[])
{
QCoreApplication app(argc, argv);
QNetworkAccessManager* manager = new QNetworkAccessManager;
QNetworkRequest req;
//这里,我访问的是tomcat服务器
req.setUrl(QUrl("http://localhost:8080/"));
manager->get(req);
QObject::connect(manager, &QNetworkAccessManager::finished, [](QNetworkReply* reply){
if(reply->error() != QNetworkReply::NoError)
{
qDebug() << "Error:" << reply->errorString();
return;
}
QByteArray buf = reply->readAll();
qDebug() << "OK:"<< buf;
});
return app.exec();
}
|
|
|
|
#include <QCoreApplication>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QJsonObject>
#include <QJsonDocument>
/**
* json 数据传输
* @brief main
* @param argc
* @param argv
* @return
*/
int main(int argc,char* argv[])
{
QCoreApplication app(argc, argv);
QNetworkAccessManager* manager = new QNetworkAccessManager;
QJsonObject obj;
obj.insert("username", QString("tuzuoquan"));
obj.insert("password", QString("tuzuoquan's password"));
QNetworkRequest req;
req.setUrl(QUrl("http://XXX/test.cgi"));
manager->post(req, QJsonDocument(obj).toJson());
QObject::connect(manager, &QNetworkAccessManager::finished, [](QNetworkReply* reply){
if(reply->error() != QNetworkReply::NoError)
{
qDebug() << "Error:" << reply->errorString();
return;
}
QByteArray buf = reply->readAll();
qDebug() << "OK:"<< buf;
});
return app.exec();
}
|
|
|
|