[SOLVED]Can download via web browser but got forbidden error if trying with my Qt code.



  • I am trying to make downloader and one of things that it should download are videos from youtube. I found out how to get exact download link to download YT video, it looks like this :
    http://r3---sn-bunjx-cune.c.youtube.com/videoplayback?sparams=algorithm,burst,cp,factor,id,ip,ipbits,itag,source,upn,expire&source=youtube&mv=m&ipbits=8&signature=9C15646EB192EAEA3ED858C59FE1855948CC22BD.03E702E0C29389D07014F9236CE3A94ADE266984&factor=1.25&key=yt1&ip=49.150.252.23&expire=1362343471&algorithm=throttle-factor&upn=N7N9OcfAVXs&burst=40&newshard=yes&fexp=900225,906335,911410,904448,914036,916611,901461,920704,912806,902000,922403,922405,929901,913605,925006,906938,931202,908529,920201,930101,906834,926403,913570,901451&sver=3&cp=U0hVR1NPUV9MS0NONV9LSVVFOkJxVHQxanR3dFhY&mt=1362320414&itag=34&id=e1b0652454a2c279&ms=au

    I tried it in web browser and it asked me if want play in vlc or save, ok I saved it, got video downloaded, everything seems good. But in my code I send get request and reply from server is: Error downloading (url) - server replied: Forbidden. I am not sure if problem is in my code only if I have to make custom request but I have no idea what to put there...

    my code:
    @
    void MainWindow::downloadRequested(){
    for(int b=0;b<urls4Download.count();b++){
    if(!folder.endsWith("/"))folder.append("/");
    QString st = FileList.at(fileIndex[b]);
    st.replace(QRegExp("/"),QString("_"));
    file=new QFile(folder+st);
    file->open(QIODevice::WriteOnly);
    reply=nam.get(QNetworkRequest(QUrl(urls4Download.at(b)))); //actually only one url in list, the one from above
    QEventLoop loop;
    connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
    connect(reply,SIGNAL(finished()),this,SLOT(saveFile()));
    connect(reply,SIGNAL(readyRead()),this,SLOT(readyReadFile()));
    loop.exec();
    }
    }
    void MainWindow::saveFile(){
    if(reply->error())QMessageBox::warning(0,"warning",reply->errorString());
    file->write(reply->readAll());
    file->flush();
    file->close();
    reply->deleteLater();
    }
    void MainWindow::readyReadFile(){
    downloadedBytes+=reply->size();
    float prog=(downloadedBytes/float(bytesToDownload))*100;
    file->write(reply->readAll());
    int progress=int(prog);
    ui->progressBar->setValue(progress);();

    }
    @

    Thanks anyone who can help me.



  • Were you able to get/output any errors?

    Anyways, some websites doesn't respond unless you set a "User Agent". I once encountered google ignoring simple search. But I was able to solve it by adding something like this:

    @QNetworkRequest request;
    request.setRawHeader("User-Agent", "MyBrowser");
    request.setUrl(/url here/);
    reply=nam.get(request);@



  • Ya you must set user agent to avoid problems :)



  • I found out my problem was with , chars parts in link, they were translated into "," as I posted it here and in browser too, after longer time I found there was my problem. But thanks anyway this user-agent header may help me later :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.