Make HTTP POST request with a list of named parameters in Qt

后端 未结 3 1012
后悔当初
后悔当初 2021-01-26 21:21

I need to make an HTTP POST request to a server from my Qt application.

The POST request would contain a list of named values, i.e. key/value pairs. They will be mostly

3条回答
  •  佛祖请我去吃肉
    2021-01-26 22:09

    QUrl params;
    
    params.addQueryItem("key1", "value1");
    params.addQueryItem("key2", "value2");
    
    QUrl resource("http://server.com/form.php");
    QNetworkAccessManager* manager = new QNetworkAccessManager(this);
    
    connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(handleEndOfRequest(QNetworkReply*)));
    
    QNetworkRequest request(resource);
    //Force Content-Type header
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    
    manager->post(request, params.encodedQuery());
    

    This code assumes that your current object is a QObject (passed as a parent for QNeworkAccessManager and slots declaration)

提交回复
热议问题