0

我有一个编码的 YouTube 网址(直接指向视频),例如:

http%253A%252F%252Fr7---sn-xjpm-q0nl.c.youtube.com%252Fvideoplayback%253Fgcr%253Dde%2526ratebypass%253Dyes%2526newshard%253Dyes%2526source%253Dyoutube%2526fexp%253D920704%25252C912806%25252C928001%25252C922403%25252C922405%25252C929901%25252C913605%25252C929104%25252C929109%25252C913546%25252C913556%25252C908493%25252C908496%25252C920201%25252C913302%25252C919009%25252C911116%25252C901451%25252C902556%2526ipbits%253D8%2526key%253Dyt1%2526id%253De4b675c403014739%2526mv%253Dm%2526cp%253DU0hUTFVTVV9LUENONF9NTVlFOlVHOGtlTmJ2WWpt%2526mt%253D1357567034%2526itag%253D46%2526ms%253Dau%2526expire%253D1357587466%2526sparams%253Dcp%25252Cgcr%25252Cid%25252Cip%25252Cipbits%25252Citag%25252Cratebypass%25252Csource%25252Cupn%25252Cexpire%2526ip%253D46.59.194.67%2526upn%253DVaZiTmBJt0Y%2526sver%253D3%26quality%3Dhd1080%26itag%3D46%26fallback_host%3Dtc.v19.cache5.c.youtube.com%26type%3Dvideo%252Fwebm%253B%2Bcodecs%253D%2522vp8.0%252C%2Bvorbis%2522%26sig%3D6C3258197CB246FA9531E056083053B1B7EAA9C2.758C7B46E77C4B5CF5C7B0C5CBEDCB5F675559D7

上面的 url 在 QString 变量“str”中,我怎样才能将它设置为 QUrl?

QUrl url = QUrl::fromPercentEncoding(str.toUtf8());

不工作!一个

qDebug() << url.toString();

将(上面)编码的 url 返回,而不是人类可显示的!

4

1 回答 1

0

您的网址被编码两次。%25是 的编码版本%。这就是为什么你也必须打fromPercentEncoding两次电话的原因。

http%253A%252F%252Fhttp%3A%2F%2F在第一轮之后和第二轮之后变为http://

例子:

#include <QApplication>
#include <QDebug>
#include <QUrl>

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

  QApplication app(argc, argv);
  QString str="http%253A%252F%252Fr7---sn-xjpm-q0nl.c.youtube.com%252Fvideoplayback%253Fgcr%253Dde%2526ratebypass%253Dyes%2526newshard%253Dyes%2526source%253Dyoutube%2526fexp%253D920704%25252C912806%25252C928001%25252C922403%25252C922405%25252C929901%25252C913605%25252C929104%25252C929109%25252C913546%25252C913556%25252C908493%25252C908496%25252C920201%25252C913302%25252C919009%25252C911116%25252C901451%25252C902556%2526ipbits%253D8%2526key%253Dyt1%2526id%253De4b675c403014739%2526mv%253Dm%2526cp%253DU0hUTFVTVV9LUENONF9NTVlFOlVHOGtlTmJ2WWpt%2526mt%253D1357567034%2526itag%253D46%2526ms%253Dau%2526expire%253D1357587466%2526sparams%253Dcp%25252Cgcr%25252Cid%25252Cip%25252Cipbits%25252Citag%25252Cratebypass%25252Csource%25252Cupn%25252Cexpire%2526ip%253D46.59.194.67%2526upn%253DVaZiTmBJt0Y%2526sver%253D3%26quality%3Dhd1080%26itag%3D46%26fallback_host%3Dtc.v19.cache5.c.youtube.com%26type%3Dvideo%252Fwebm%253B%2Bcodecs%253D%2522vp8.0%252C%2Bvorbis%2522%26sig%3D6C3258197CB246FA9531E056083053B1B7EAA9C2.758C7B46E77C4B5CF5C7B0C5CBEDCB5F675559D7";
  qDebug() << QUrl::fromPercentEncoding(str.toUtf8());
  qDebug() << QUrl::fromPercentEncoding(QUrl::fromPercentEncoding(str.toUtf8()).toUtf8());
  return app.exec();
}
于 2013-01-07T19:26:18.493 回答