使用`QNetworkAccessManager`发布请求

时间:2014-03-04 05:14:08

标签: qt network-programming

我对网站使用QNetworkAccessManager发布请求:

    void Spider::getProducts()
    {
        connect(&manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(getProducts(QNetworkReply*)));

        request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");

        request.setUrl(QUrl("http://www.example.com/query"));

        for(int i = 0; i < categories.size(); ++i)
        {
            if(categories[i].isCategory())
            {
                isSubCategory = false;

                emit manager.finished(reply);
            }
            else
            {
                for(int page_number = 0; page_number < categories[i].getPageCount(); ++i)
                {
                    isSubCategory = true;

                    QJsonObject json;

                    json.insert("NValue", categories[i].getNValue());
                    json.insert("NodeId", categories[i].getNodeId());
                    json.insert("StoreId", categories[i].getStoreId());
                    json.insert("StoreType", categories[i].getStoreType());
                    json.insert("PageNumber", ++page_number);
                    json.insert("SubCategoryId", categories[i].getSubCategoryId());

                    QJsonDocument doc;

                    doc.setObject(json);

                    QByteArray request_body = doc.toJson();

                    manager.post(request, request_body);
                }
            }
        }
    }

当我运行这个程序时,在开始时,该程序正常运行,运行一段时间后,它将停止:既不终止也不继续运行。我无法弄清楚为什么这样的行为?使用QNetworkAccess时有什么需要注意的吗?或者我被那个网站拒绝了? ...

1 个答案:

答案 0 :(得分:0)

您在循环中多次使用相同的QNetworkAccessManager对象。这是不正确的。对于每个单独的发布请求,如果要并行发送请求,则需要单独的QNetworkAccessManager对象。否则,如果您只想使用单个实例,那么当前一个请求在完成的广告位中结束时,您需要通过发送下一个帖子请求来序列化您的帖子请求。

更新:试用此代码,它使用单个QNetworkAccessManager对象,您需要根据需要检查i ++的正确位置

connect(&manager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(getProducts(QNetworkReply*))); // add this to constructor of your program

void Spider::getProducts()
    {
        static int i = 0;
        request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");

        request.setUrl(QUrl("http://www.example.com/query"));

        while(categories[i].isCategory())
        {
            isSubCategory = false;
            i++;
        }
        if(i == categories.size())
        {
           emit allPostRequestsDone();
           return;
        }

            else
            {
                i++;
                if(page_number < categories[i].getPageCount())
                {
                    isSubCategory = true;

                    QJsonObject json;

                    json.insert("NValue", categories[i].getNValue());
                    json.insert("NodeId", categories[i].getNodeId());
                    json.insert("StoreId", categories[i].getStoreId());
                    json.insert("StoreType", categories[i].getStoreType());
                    json.insert("PageNumber", ++page_number);
                    json.insert("SubCategoryId", categories[i].getSubCategoryId());

                    QJsonDocument doc;

                    doc.setObject(json);

                    QByteArray request_body = doc.toJson();

                    manager.post(request, request_body);
                }
            }
        }
    }