示例#1
0
文件: formpost.cpp 项目: A5aFk/Hotot
QNetworkReply * FormPost::postData(QNetworkRequest& request)
{
    QString crlf = "\r\n";
    qsrand(QDateTime::currentDateTime().toTime_t());
    QString b = QVariant(qrand()).toString() + QVariant(qrand()).toString()
                + QVariant(qrand()).toString();
    QString boundary = "---------------------------" + b;
    QString endBoundary = crlf + "--" + boundary + "--" + crlf;
    QString contentType = "multipart/form-data; boundary=" + boundary;
    boundary = "--" + boundary + crlf;
    QByteArray bond = boundary.toLatin1();
    QByteArray send;
    bool first = true;

    for (int i = 0; i < fieldNames.size(); i++) {
        send.append(bond);
        if (first) {
            boundary = crlf + boundary;
            bond = boundary.toLatin1();
            first = false;
        }
        send.append(QString("Content-Disposition: form-data; name=\""
                            + fieldNames.at(i) + "\"" + crlf).toLatin1());
        if (encodingS == "utf-8") send.append(QString("Content-Transfer-Encoding: 8bit"
                                                  + crlf).toLatin1());
        send.append(crlf.toLatin1());
        send.append(strToEnc(fieldValues.at(i)));
    }
    for (int i = 0; i < files.size(); i++) {
        send.append(bond);
        send.append(QString("Content-Disposition: form-data; name=\""
                            + fileFieldNames.at(i) + "\"; filename=\""
                            + fileNames.at(i) + "\"" + crlf).toLatin1());
        send.append(QString("Content-Type: " + fileMimes.at(i) + crlf + crlf).toLatin1());
        send.append(files.at(i));
    }

    send.append(endBoundary.toLatin1());

    fieldNames.clear();
    fieldValues.clear();
    fileFieldNames.clear();
    fileNames.clear();
    fileMimes.clear();
    files.clear();


    connect(http, SIGNAL(finished(QNetworkReply *)), this, SLOT(readData(QNetworkReply *)));
    if (userAgentS != "") request.setRawHeader("User-Agent", userAgentS.toLatin1());
    if (refererS != "") request.setRawHeader("Referer", refererS.toLatin1());
    request.setHeader(QNetworkRequest::ContentTypeHeader, contentType.toLatin1());
    request.setHeader(QNetworkRequest::ContentLengthHeader, QVariant(send.size()).toString());
    QNetworkReply * reply = http->post(request, send);
    return reply;
}
示例#2
0
QNetworkReply * FormPost::postData(QString url) {
  QString host;
  host=url.right(url.length()-url.indexOf("://")-3);
  host=host.left(host.indexOf("/"));
  QString crlf="\r\n";
  qsrand(QDateTime::currentDateTime().toTime_t());
  QString b=QVariant(qrand()).toString()+QVariant(qrand()).toString()+QVariant(qrand()).toString();
  QString boundary="---------------------------"+b;
  QString endBoundary=crlf+"--"+boundary+"--"+crlf;
  QString contentType="multipart/form-data; boundary="+boundary;
  boundary="--"+boundary+crlf;
  QByteArray bond=boundary.toLatin1();
  QByteArray send;
  bool first=true;

  for (int i=0; i<_fieldNames.size(); i++) {
    send.append(bond);

    if (first) {
      boundary=crlf+boundary;
      bond=boundary.toLatin1();
      first=false;
    }

    send.append(QString("Content-Disposition: form-data; name=\""+_fieldNames.at(i)+"\""+crlf).toLatin1());

    if (_encoding=="utf-8") send.append(QString("Content-Transfer-Encoding: 8bit"+crlf).toLatin1());

    send.append(crlf.toLatin1());
    send.append(strToEnc(_fieldValues.at(i)));
  }

  for (int i=0; i<files.size(); i++) {
    send.append(bond);
    send.append(QString("Content-Disposition: form-data; name=\""+_fileFieldNames.at(i)+"\"; filename=\""+_fileNames.at(i)+"\""+crlf).toLatin1());
    send.append(QString("Content-Type: "+_fileMimes.at(i)+crlf+crlf).toLatin1());
    send.append(files.at(i));
  }

  send.append(endBoundary.toLatin1());

  _fieldNames.clear();
  _fieldValues.clear();
  _fileFieldNames.clear();
  _fileNames.clear();
  _fileMimes.clear();
  files.clear();

  QNetworkAccessManager * http=new QNetworkAccessManager(this);
  connect(http,SIGNAL(finished(QNetworkReply *)),this,SLOT(readData(QNetworkReply *)));
  QNetworkRequest request;
  request.setRawHeader("Host", host.toLatin1());

  if (_userAgent!="") request.setRawHeader("User-Agent", _userAgent.toLatin1());

  if (_referer!="") request.setRawHeader("Referer", _referer.toLatin1());

  request.setHeader(QNetworkRequest::ContentTypeHeader, contentType.toLatin1());
  request.setHeader(QNetworkRequest::ContentLengthHeader, QVariant(send.size()).toString());
  request.setUrl(QUrl(url));
  QNetworkReply * reply=http->post(request,send);
  return reply;
}
//загрузка файла
void guBookUploader::uploadEbookData(ebook uploadEbook)
{
    QString uploadedMd5 = uploadEbook.MD5;
    //test
    serviceUrl = "http://localhost/lgws/service.php";
    QNetworkRequest *uploadEbookRequest;
    //TODO: вести лог
    uploadEbookRequest = new QNetworkRequest(QUrl(serviceUrl));
    QByteArray fileToSend; // byte array to be sent in POST
    if(!uploadEbook.inLib)
    {
        QFile uploadFile(uploadEbook.filePath);
        if (!uploadFile.open(QIODevice::ReadOnly))
        {
            //TODO: дать сигнал о том, что закачка провалилась так как не удалось открыть файл
            return;
        }

        fileToSend = uploadFile.readAll();
        uploadFile.close(); //расскомментировать
    }

    ////////////////////начало кода, взятого из "formpost"
    QString encodingS;
    encodingS = "utf-8";
    QString userAgentS;
    QString refererS;
    QStringList fieldNames;
    QStringList fieldValues;
    QStringList fileFieldNames;
    QStringList fileNames;
    QStringList fileMimes;
    QList<QByteArray> files;

    fieldNames << "xml";
    fieldValues << sendEbookInfoXML(uploadEbook);


    if(!uploadEbook.inLib)
    {
        fieldNames << "MAX_FILE_SIZE";
        fieldValues << QString::number(maxUploadFileSize);

        fileFieldNames << "uploadedfile";
        fileNames << uploadEbook.fileName +"."+ uploadEbook.fileExtension;

        fileMimes << "application/octet-stream";
        files << fileToSend;
    }
    QString crlf="\r\n";
    qsrand(QDateTime::currentDateTime().toTime_t());
    QString b=QVariant(qrand()).toString()+QVariant(qrand()).toString()+QVariant(qrand()).toString();
    QString boundary="---------------------------"+b;
    QString endBoundary=crlf+"--"+boundary+"--"+crlf;
    QString contentType="multipart/form-data; boundary="+boundary;
    boundary="--"+boundary+crlf;
    QByteArray bond=boundary.toAscii();
    QByteArray send;
    bool first=true;
    for (int i=0; i<fieldNames.size(); i++) {
        send.append(bond);
        if (first) {
            boundary=crlf+boundary;
            bond=boundary.toAscii();
            first=false;
        }
        send.append(QString("Content-Disposition: form-data; name=\""+fieldNames.at(i)+"\""+crlf).toAscii());
        if (encodingS=="utf-8") send.append(QString("Content-Transfer-Encoding: 8bit"+crlf).toAscii());
        send.append(crlf.toAscii());
        send.append(strToEnc(fieldValues.at(i),encodingS));
    }
    for (int i=0; i<files.size(); i++) {
        send.append(bond);
        send.append(QString("Content-Disposition: form-data; name=\""+fileFieldNames.at(i)+"\"; filename=\""+fileNames.at(i)+"\""+crlf).toAscii());
        send.append(QString("Content-Type: "+fileMimes.at(i)+crlf+crlf).toAscii());
        send.append(files.at(i));
    }

    send.append(endBoundary.toAscii());
    uploadEbookRequest->setHeader(QNetworkRequest::ContentLengthHeader, QVariant(send.size()).toString());
    uploadEbookRequest->setHeader(QNetworkRequest::ContentTypeHeader, contentType.toAscii());
    uploadEbookRequest->setRawHeader("Accept","text/html,text/xml,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    uploadEbookRequest->setRawHeader("Accept-Charset","windows-1251,utf-8;q=0.7,*;q=0.7");
    uploadEbookRequest->setRawHeader("Accept-Encoding", "gzip,deflate");//,qcompress
    uploadEbookRequest->setRawHeader("Authorization", "Basic " + QByteArray(QString("%1:%2").arg("genesis").arg("upload").toAscii()).toBase64());

    uploadEbookRequest->setRawHeader("Connection","keep-alive");
    uploadEbookRequest->setRawHeader("Keep-Alive","115");
    uploadEbookRequest->setRawHeader("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2) Gecko/20100115 Firefox/3.6");
    QString host("free-books.dontexist.org");
    uploadEbookRequest->setRawHeader("Host",host.toAscii());
    uploadEbookRequest->setRawHeader("Referer","http://free-books.dontexist.com/librarian/");

    uploadEbookReply  *uplEbookReplyManager;
    QNetworkReply  *uplEbookReply;

    //    QFile file("out.txt");
    //        if (!file.open(QIODevice::WriteOnly))
    //            return;
    //        file.write(send);
    //        file.close();
    uplEbookReply = httpUploadFilesManager->post(*uploadEbookRequest, send);
    //send.clear();
    uplEbookReplyManager = new uploadEbookReply(uplEbookReply , uploadedMd5, this);
    connect(uplEbookReplyManager, SIGNAL(onUploadEbookProcess(QString,int)), this, SLOT(onUploadProgress(QString,int)));
    //log(uploadEbookRequest->rawHeaderList());
    uplEbookReply->setProperty("MD5",uploadedMd5);
    qDebug() << "MD5 setted";
    if(uploadEbook.inLib)
    {
         uplEbookReply->setProperty("type", "edit");
    }
    else
    {
         uplEbookReply->setProperty("type", "upload");
    }

    // // connections
    //connect(reply,SIGNAL(uploadProgress(qint64,qint64)),SLOT(mySetValue(qint64,qint64)));
    //connect(reply,SIGNAL(finished()),SLOT(replyFinished())); // reply finished - close file
    //fillBibliographyReply->setProperty("dataSource",dataSource);
    //numOfFillBibliographyConnections++;
}