2 u010075251 u010075251 于 2014.02.10 13:37 提问

QT post指令如何立刻执行

事件描述:客户端需要向服务器POST用户名和密码,如果密码正确,服务器会返还一个随机数,然后客户端将这个随机数返回给服务器,就可以进入服务器的下一级目录,上传数据过去。

问题:发送第一个POST指令时(用户名和密码),服务器没有立刻执行触发SLOT函数这个时候无法获取随机数,而时等到整个代码跑完时才触发SLOT函数,这样第二个POST指令必须被放入SLOT函数中,程序定义了这个函数,connect(qnam, SIGNAL(finished(QNetworkReply*)), this, SLOT(finishedSlot(QNetworkReply*)));导致每一个POST完成后,都会调用SLOT,程序限入了死循环。。

关键代码如下:
void HttpWindow::downloadFile()
{
url = urlLineEdit->text();
QNetworkRequest networkrequest;
networkrequest.setUrl(url);
QByteArray append("username=root&password=");
qnam->post(networkrequest, append);//我想在这里调SLOT函数,获取随机数,然后将下面的SLOT函数中的指令全部移到下面来。
qnam->post(
}
void HttpWindow::finishedSlot(QNetworkReply *reply)
{
QString tmp1;

if (reply->error() == QNetworkReply::NoError)
{
    QList<QNetworkReply::RawHeaderPair> tmp= reply->rawHeaderPairs();
    for(int i=0;i<tmp.size();i++)
    {
        QNetworkReply::RawHeaderPair pair = tmp[i];
        QByteArray bytes = pair.second;
        tmp1 =  QString::fromUtf8(bytes);
        int pos=tmp1.indexOf("stok=");
        if(pos!=-1)
        {
           stok=tmp1.mid(pos,pos+32);
           QString firstAdd("http://192.168.1.1/cgi-bin/luci/;");
           QString secondAdd("/admin/system/flashops");
           stok+=secondAdd;
           firstAdd+=stok;
           QNetworkRequest networkrequest;
           networkrequest.setUrl(firstAdd);
           QFile software("E:/software/openwrt-ar71xx-generic-db120-squashfs-sysupgrade.bin");
           if(software.open(QIODevice::ReadOnly))
           {
               QByteArray formData;
               QString _boundary="---------------------------7de3e3910060e";
               QString first_boundary="content-Type: multipart/form-data; boundary=";
               first_boundary+=_boundary;
               reply=qnam->post(networkrequest, first_boundary.toAscii());
               QString content_Disposition="Content-Disposition: form-data; name=\"keep\"\r\n\r\n";
               qnam->post(networkrequest, content_Disposition.toAscii());
               QString ON="on\r\n";
               reply=qnam->post(networkrequest, ON.toAscii());
               reply=qnam->post(networkrequest, _boundary.toAscii());

               QString Content_Disposition("Content-Disposition: form-data;  name=\"D:\\software\\openwrt-ar71xx-generic-db120-squashfs-sysupgrade.bin\"");
               qnam->post(networkrequest, Content_Disposition.toAscii());

               //formData.append(QString("--"+_boundary+"\r\n").toAscii());
               //formData.append(QString("Content-Disposition: form-data;  name=\"D:\\software\\openwrt-ar71xx-generic-db120-squashfs-sysupgrade.bin\"").toUtf8());
               formData.append(software.readAll());
               reply=qnam->post(networkrequest, formData);
               reply=qnam->post(networkrequest, QString("\r\n").toAscii());
               reply=qnam->post(networkrequest, _boundary.toAscii());

               //formData.append(QString("\r\n").toAscii());
               //formData.append(QString("--"+_boundary+"\r\n").toAscii());
               //int contentLength=formData.size();
               //networkrequest.setHeader(QNetworkRequest::ContentTypeHeader,contenttype);
               //networkrequest.setHeader(QNetworkRequest::ContentLengthHeader,contentLength);
                            }
           else
               return;


            break;
        }
    }
}
else
{
    return ;
}

reply->deleteLater();
}

Csdn user default icon
上传中...
上传图片
插入图片