Qt:http请求get和post

 转载自:Qt实现HTTP的Get/Post请求_while(1) { smile(); }-CSDN博客

PRO文件添加这两行

QT += network
QT += xml

头文件

#include 
#include 
#include 

 声明:


    void requestFinished(QNetworkReply*);

触发

void QtGuiApplication::onBtnGetClicked() {
	QNetworkRequest request;
	QNetworkAccessManager* naManager = new QNetworkAccessManager(this);
	QMetaObject::Connection connRet = QObject::connect(naManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestFinished(QNetworkReply*)));
	Q_ASSERT(connRet);

	request.setUrl(QUrl("https://www.baidu.com"));
	QNetworkReply* reply = naManager->get(request);
}

槽函数

void QtGuiApplication::requestFinished(QNetworkReply* reply) {
	// 获取http状态码
	QVariant statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
	if(statusCode.isValid())
		qDebug() << "status code=" << statusCode.toInt();

	QVariant reason = reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
	if(reason.isValid())
		qDebug() << "reason=" << reason.toString();

	QNetworkReply::NetworkError err = reply->error();
	if(err != QNetworkReply::NoError) {
		qDebug() << "Failed: " << reply->errorString();
	}
	else {
		// 获取返回内容
		qDebug() << reply->readAll();
	}
}

post

void QtGuiApplication::onBtnPushClicked() {
	QNetworkRequest request;
	QNetworkAccessManager* naManager = new QNetworkAccessManager(this);
	QMetaObject::Connection connRet = QObject::connect(naManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestFinished(QNetworkReply*)));
	Q_ASSERT(connRet);

	request.setUrl(QUrl("https://www.baidu.com"));
	
	QString testData = "test";
	QNetworkReply* reply = naManager->post(request, testData.toUtf8());
}
void QtGuiApplication::requestFinished(QNetworkReply* reply) {
	// 获取http状态码
	QVariant statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
	if(statusCode.isValid())
		qDebug() << "status code=" << statusCode.toInt();

	QVariant reason = reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
	if(reason.isValid())
		qDebug() << "reason=" << reason.toString();

	QNetworkReply::NetworkError err = reply->error();
	if(err != QNetworkReply::NoError) {
		qDebug() << "Failed: " << reply->errorString();
	}
	else {
		// 获取返回内容
		qDebug() << reply->readAll();
	}
}

2023年2月17日补充

关于post请求我今天遇到一个问题,就是post带请求体的时候,怎么都不成功,最终解决了,需要在请求头里面加两行代码

void login02::on_pushButtonSignIn_clicked()
{

   QJsonObject object;
   object.insert("username", ui->lineEditUsername->text());
   object.insert("password", ui->lineEditPassword->text());
   QJsonDocument doc;
   doc.setObject(object);
   QByteArray ba = doc.toJson(QJsonDocument::Compact);


    //登录
    QNetworkRequest request;
    QNetworkAccessManager* naManager = new QNetworkAccessManager(this);
    QMetaObject::Connection connRet = QObject::connect(naManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestFinished(QNetworkReply*)));
    Q_ASSERT(connRet);
    request.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("application/x-www-form-urlencoded"));
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json;charset=UTF-8");
    request.setUrl(QUrl(constant->_HOST_SIGNIN));
    QNetworkReply* reply = naManager->post(request, ba);

}

你可能感兴趣的:(Qt,qt)