[qt] Jak obsłużyc złożone adresy uri


(Oski225) #1

Tak jak w temacie. Gdy próbuje pobrać np. plik:

http://v8.lscache7.googlevideo.com/videoplayback?ip=0.0.0.0&sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Cburst%2Cfactor&itag=5&ipbits=0&signature=BAC2468DB75DA81FA1CF367B89C5FDB85AA2E509.C1F29CAAEC862FC4BBC489BDF57F8B244DEBF20E&sver=3&expire=1248732000&key=yt1&factor=1.25&burst=40&id=02c6d59d5c2f612c

Zamiast tego pobiera się coś, jakbym pobierał taki plik:

http://v8.lscache7.googlevideo.com/videoplayback

Czyli bez dodatkowych parametrów. Wie ktoś jak włączyć do programu te parametry?


(_chris_) #2

Witam,

i prawidłowo się pobiera. Wszystko, co jest za videoplayback to parametry dla wywoływanego skryptu php, a nie elementy adresu pliku.


(Oski225) #3

Wiesz w takim razie jak wygenerować bezpośredni link do pliku *.flv?

Albo jak obsłużyć takie żądanie


(Sawyer47) #4

Ale gdzie masz ten link? Np. w labelce między (Tak się chyba włącza parsowanie kodu w tekście, tak? Mogło się coś zmienić, bo to pamiętam z czasów Qt 4.2) czy w obiekcie QUrl?


(Oski225) #5

W obiekcie QUrl


(Sawyer47) #6

Mógłbyś pokazać kawałek kodu? Z konkretnym kodem o wiele prościej pomóc.


(Oski225) #7

Sprawdzałem działanie na przykładowym programie, który jest już w bilbiotece qt:

httpwindow.h

#ifndef HTTPWINDOW_H

#define HTTPWINDOW_H


#include 


QT_BEGIN_NAMESPACE

class QDialogButtonBox;

class QFile;

class QHttp;

class QHttpResponseHeader;

class QLabel;

class QLineEdit;

class QProgressDialog;

class QPushButton;

class QSslError;

class QAuthenticator;

QT_END_NAMESPACE


class HttpWindow : public QDialog

{

    Q_OBJECT


public:

    HttpWindow(QWidget *parent = 0);


private slots:

    void downloadFile();

    void cancelDownload();

    void httpRequestFinished(int requestId, bool error);

    void readResponseHeader(const QHttpResponseHeader &responseHeader);

    void updateDataReadProgress(int bytesRead, int totalBytes);

    void enableDownloadButton();

    void slotAuthenticationRequired(const QString &, quint16, QAuthenticator *);

#ifndef QT_NO_OPENSSL

    void sslErrors(const QList &errors);

#endif


private:

    QLabel *statusLabel;

    QLabel *urlLabel;

    QLineEdit *urlLineEdit;

    QProgressDialog *progressDialog;

    QPushButton *downloadButton;

    QPushButton *quitButton;

    QDialogButtonBox *buttonBox;


    QHttp *http;

    QFile *file;

    int httpGetId;

    bool httpRequestAborted;

};


#endif

httpwindow.cpp:

#include 

#include 


#include "httpwindow.h"

#include "ui_authenticationdialog.h"


HttpWindow::HttpWindow(QWidget *parent)

    : QDialog(parent)

{

#ifndef QT_NO_OPENSSL

    urlLineEdit = new QLineEdit("https://");

#else

    urlLineEdit = new QLineEdit("http://");

#endif


    urlLabel = new QLabel(tr("&URL:"));

    urlLabel->setBuddy(urlLineEdit);

    statusLabel = new QLabel(tr("Please enter the URL of a file you want to "

                                "download."));


    downloadButton = new QPushButton(tr("Download"));

    downloadButton->setDefault(true);

    quitButton = new QPushButton(tr("Quit"));

    quitButton->setAutoDefault(false);


    buttonBox = new QDialogButtonBox;

    buttonBox->addButton(downloadButton, QDialogButtonBox::ActionRole);

    buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);


    progressDialog = new QProgressDialog(this);


    http = new QHttp(this);


    connect(urlLineEdit, SIGNAL(textChanged(const QString &)),

            this, SLOT(enableDownloadButton()));

    connect(http, SIGNAL(requestFinished(int, bool)),

            this, SLOT(httpRequestFinished(int, bool)));

    connect(http, SIGNAL(dataReadProgress(int, int)),

            this, SLOT(updateDataReadProgress(int, int)));

    connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)),

            this, SLOT(readResponseHeader(const QHttpResponseHeader &)));

    connect(http, SIGNAL(authenticationRequired(const QString &, quint16, QAuthenticator *)),

            this, SLOT(slotAuthenticationRequired(const QString &, quint16, QAuthenticator *)));

#ifndef QT_NO_OPENSSL

    connect(http, SIGNAL(sslErrors(const QList &)),

            this, SLOT(sslErrors(const QList &)));

#endif

    connect(progressDialog, SIGNAL(canceled()), this, SLOT(cancelDownload()));

    connect(downloadButton, SIGNAL(clicked()), this, SLOT(downloadFile()));

    connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));


    QHBoxLayout *topLayout = new QHBoxLayout;

    topLayout->addWidget(urlLabel);

    topLayout->addWidget(urlLineEdit);


    QVBoxLayout *mainLayout = new QVBoxLayout;

    mainLayout->addLayout(topLayout);

    mainLayout->addWidget(statusLabel);

    mainLayout->addWidget(buttonBox);

    setLayout(mainLayout);


    setWindowTitle(tr("HTTP"));

    urlLineEdit->setFocus();

}


void HttpWindow::downloadFile()

{

    QUrl url(urlLineEdit->text());

    QFileInfo fileInfo(url.path());

    QString fileName = fileInfo.fileName();

    if (fileName.isEmpty())

        fileName = "index.html";


    if (QFile::exists(fileName)) {

        if (QMessageBox::question(this, tr("HTTP"), 

                                  tr("There already exists a file called %1 in "

                                     "the current directory. Overwrite?").arg(fileName),

                                  QMessageBox::Ok|QMessageBox::Cancel, QMessageBox::Cancel)

            == QMessageBox::Cancel)

            return;

        QFile::remove(fileName);

    }


    file = new QFile(fileName);

    if (!file->open(QIODevice::WriteOnly)) {

        QMessageBox::information(this, tr("HTTP"),

                                 tr("Unable to save the file %1: %2.")

                                 .arg(fileName).arg(file->errorString()));

        delete file;

        file = 0;

        return;

    }


    QHttp::ConnectionMode mode = url.scheme().toLower() == "https" ? QHttp::ConnectionModeHttps : QHttp::ConnectionModeHttp;

    http->setHost(url.host(), mode, url.port() == -1 ? 0 : url.port());


    if (!url.userName().isEmpty())

        http->setUser(url.userName(), url.password());


    httpRequestAborted = false;

    QByteArray path = QUrl::toPercentEncoding(url.path(), "!$&'()*+,;=:@/");

    if (path.isEmpty())

        path = "/";

    httpGetId = http->get(path, file);


    progressDialog->setWindowTitle(tr("HTTP"));

    progressDialog->setLabelText(tr("Downloading %1.").arg(fileName));

    downloadButton->setEnabled(false);

}


void HttpWindow::cancelDownload()

{

    statusLabel->setText(tr("Download canceled."));

    httpRequestAborted = true;

    http->abort();

    downloadButton->setEnabled(true);

}


void HttpWindow::httpRequestFinished(int requestId, bool error)

{

    if (requestId != httpGetId)

        return;

    if (httpRequestAborted) {

        if (file) {

            file->close();

            file->remove();

            delete file;

            file = 0;

        }


        progressDialog->hide();

        return;

    }


    if (requestId != httpGetId)

        return;


    progressDialog->hide();

    file->close();


    if (error) {

        file->remove();

        QMessageBox::information(this, tr("HTTP"),

                                 tr("Download failed: %1.")

                                 .arg(http->errorString()));

    } else {

        QString fileName = QFileInfo(QUrl(urlLineEdit->text()).path()).fileName();

        statusLabel->setText(tr("Downloaded %1 to current directory.").arg(fileName));

    }


    downloadButton->setEnabled(true);

    delete file;

    file = 0;

}


void HttpWindow::readResponseHeader(const QHttpResponseHeader &responseHeader)

{

    switch (responseHeader.statusCode()) {

    case 200: // Ok

    case 301: // Moved Permanently

    case 302: // Found

    case 303: // See Other

    case 307: // Temporary Redirect

        // these are not error conditions

        break;


    default:

        QMessageBox::information(this, tr("HTTP"),

                                 tr("Download failed: %1.")

                                 .arg(responseHeader.reasonPhrase()));

        httpRequestAborted = true;

        progressDialog->hide();

        http->abort();

    }

}


void HttpWindow::updateDataReadProgress(int bytesRead, int totalBytes)

{

    if (httpRequestAborted)

        return;


    progressDialog->setMaximum(totalBytes);

    progressDialog->setValue(bytesRead);

}


void HttpWindow::enableDownloadButton()

{

    downloadButton->setEnabled(!urlLineEdit->text().isEmpty());

}


void HttpWindow::slotAuthenticationRequired(const QString &hostName, quint16, QAuthenticator *authenticator)

{

    QDialog dlg;

    Ui::Dialog ui;

    ui.setupUi(&dlg);

    dlg.adjustSize();

    ui.siteDescription->setText(tr("%1 at %2").arg(authenticator->realm()).arg(hostName));


    if (dlg.exec() == QDialog::Accepted) {

        authenticator->setUser(ui.userEdit->text());

        authenticator->setPassword(ui.passwordEdit->text());

    }

}


#ifndef QT_NO_OPENSSL

void HttpWindow::sslErrors(const QList &errors)

{

    QString errorString;

    foreach (const QSslError &error, errors) {

        if (!errorString.isEmpty())

            errorString += ", ";

        errorString += error.errorString();

    }


    if (QMessageBox::warning(this, tr("HTTP Example"),

                             tr("One or more SSL errors has occurred: %1").arg(errorString),

                             QMessageBox::Ignore | QMessageBox::Abort) == QMessageBox::Ignore) {

        http->ignoreSslErrors();

    }

}

#endif

main.cpp:

#include 


#include "httpwindow.h"


int main(int argc, char *argv[])

{

    QApplication app(argc, argv);

    HttpWindow httpWin;

    httpWin.show();

    return httpWin.exec();

}

ui_authenticationdialog.h

#ifndef UI_AUTHENTICATIONDIALOG_H

#define UI_AUTHENTICATIONDIALOG_H


#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 


QT_BEGIN_NAMESPACE


class Ui_Dialog

{

public:

    QGridLayout *gridLayout;

    QLabel *label;

    QLabel *label_2;

    QLineEdit *userEdit;

    QLabel *label_3;

    QLineEdit *passwordEdit;

    QDialogButtonBox *buttonBox;

    QLabel *label_4;

    QLabel *siteDescription;

    QSpacerItem *spacerItem;


    void setupUi(QDialog *Dialog)

    {

        if (Dialog->objectName().isEmpty())

            Dialog->setObjectName(QString::fromUtf8("Dialog"));

        Dialog->resize(389, 243);

        gridLayout = new QGridLayout(Dialog);

        gridLayout->setObjectName(QString::fromUtf8("gridLayout"));

        label = new QLabel(Dialog);

        label->setObjectName(QString::fromUtf8("label"));

        label->setWordWrap(false);


        gridLayout->addWidget(label, 0, 0, 1, 2);


        label_2 = new QLabel(Dialog);

        label_2->setObjectName(QString::fromUtf8("label_2"));


        gridLayout->addWidget(label_2, 2, 0, 1, 1);


        userEdit = new QLineEdit(Dialog);

        userEdit->setObjectName(QString::fromUtf8("userEdit"));


        gridLayout->addWidget(userEdit, 2, 1, 1, 1);


        label_3 = new QLabel(Dialog);

        label_3->setObjectName(QString::fromUtf8("label_3"));


        gridLayout->addWidget(label_3, 3, 0, 1, 1);


        passwordEdit = new QLineEdit(Dialog);

        passwordEdit->setObjectName(QString::fromUtf8("passwordEdit"));


        gridLayout->addWidget(passwordEdit, 3, 1, 1, 1);


        buttonBox = new QDialogButtonBox(Dialog);

        buttonBox->setObjectName(QString::fromUtf8("buttonBox"));

        buttonBox->setOrientation(Qt::Horizontal);

        buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);


        gridLayout->addWidget(buttonBox, 5, 0, 1, 2);


        label_4 = new QLabel(Dialog);

        label_4->setObjectName(QString::fromUtf8("label_4"));


        gridLayout->addWidget(label_4, 1, 0, 1, 1);


        siteDescription = new QLabel(Dialog);

        siteDescription->setObjectName(QString::fromUtf8("siteDescription"));

        QFont font;

        font.setBold(true);

        font.setWeight(75);

        siteDescription->setFont(font);

        siteDescription->setWordWrap(true);


        gridLayout->addWidget(siteDescription, 1, 1, 1, 1);


        spacerItem = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);


        gridLayout->addItem(spacerItem, 4, 0, 1, 1);



        retranslateUi(Dialog);

        QObject::connect(buttonBox, SIGNAL(accepted()), Dialog, SLOT(accept()));

        QObject::connect(buttonBox, SIGNAL(rejected()), Dialog, SLOT(reject()));


        QMetaObject::connectSlotsByName(Dialog);

    } // setupUi


    void retranslateUi(QDialog *Dialog)

    {

        Dialog->setWindowTitle(QApplication::translate("Dialog", "Http authentication required", 0, QApplication::UnicodeUTF8));

        label->setText(QApplication::translate("Dialog", "You need to supply a Username and a Password to access this site", 0, QApplication::UnicodeUTF8));

        label_2->setText(QApplication::translate("Dialog", "Username:", 0, QApplication::UnicodeUTF8));

        label_3->setText(QApplication::translate("Dialog", "Password:", 0, QApplication::UnicodeUTF8));

        label_4->setText(QApplication::translate("Dialog", "Site:", 0, QApplication::UnicodeUTF8));

        siteDescription->setText(QApplication::translate("Dialog", "%1 at %2", 0, QApplication::UnicodeUTF8));

        Q_UNUSED(Dialog);

    } // retranslateUi


};


namespace Ui {

    class Dialog: public Ui_Dialog {};

} // namespace Ui


QT_END_NAMESPACE


#endif // UI_AUTHENTICATIONDIALOG_H