QT POST/GET HTTP操作

工程文件

Qt += network

举例

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    QNetworkAccessManager *accessManager = new QNetworkAccessManager(this);
    connect(accessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(finishedSlot(QNetworkReply*)));

	QNetworkRequest request;
    request.setUrl(QUrl("127.0.0.1"));

	//get
	accessManager->get(request);

    QByteArray postData;
    postData.append("username=admin&password=123456");

	//post
    QNetworkReply* reply = accessManager->post(request, postData);
}

void Widget::finishedSlot(QNetworkReply *reply)
{
     if (reply->error() == QNetworkReply::NoError)
     {
         QByteArray bytes = reply->readAll();
         qDebug() << bytes;
     }
     else
     {
         qDebug() << "finishedSlot errors here";
         qDebug( "found error .... code: %d\n", (int)reply->error());
         qDebug(qPrintable(reply->errorString()));
     }
     reply->deleteLater();
}
posted @ 2018-08-13 10:38  thomas_blog  阅读(1082)  评论(0编辑  收藏  举报