QUrl的使用,特別是對含特殊字符的字符串進行 URL 格式化編碼

QUrl提取與寫入參數

QUrl url("www.baidu.com?a=666&b=888"); url.addQueryItem("c","123456"); qDebug()<<url.queryItemValue("b"); qDebug()<<url.toString();

轉自:https://blog.csdn.net/Think88666/article/details/84066915編碼

 

網址URL中特殊字符轉義編碼

 

字符    -    URL編碼值url

空格    -    %20
"          -    %22
#         -    %23
%        -    %25
&         -    %26
(          -    %28
)          -    %29
+         -    %2B
,          -    %2C
/          -    %2F
:          -    %3A
;          -    %3B
<         -    %3C
=         -    %3D
>         -    %3E
?         -    %3F
@       -    %40
\          -    %5C
|          -    %7C spa

 

URL特殊字符轉義,URL中一些字符的特殊含義,基本編碼規則以下:.net

一、空格換成加號(+)
二、正斜槓(/)分隔目錄和子目錄
三、問號(?)分隔URL和查詢
四、百分號(%)制定特殊字符
五、#號指定書籤
六、&號分隔參數

code

若是須要在URL中用到,須要將這些特殊字符換成相應的十六進制的值
+     %2B
/      %2F
?     %3F
%    %25
#     %23
&    %26blog

 

Qt 中使用 QUrl 對字符串進行 URL 格式化編碼

QUrl 爲咱們提供了不少的便利方法,其中對字符串進行 URL 格式化編碼的方法文檔

QByteArray QUrl::toPercentEncoding(const QString & input, const QByteArray & exclude = QByteArray(), const QByteArray & include = QByteArray()) [static]

就是一個很方便的方法,在這個方法中,咱們能夠簡單地對字符串進行編碼,也能夠經過指定第二個參數 exclude 指定哪些字符不須要編碼,以及指定第三個參數 include 強制將某些字符進行編碼。字符串

下面是 Qt 文檔中的一個實例:input

QByteArray ba = QUrl::toPercentEncoding("{a fishy string?}", "{}", "s");
qDebug(ba.constData());
// prints "{a fi%73hy %73tring%3F}"

下面是一個簡單的使用實例:string

#include <QCoreApplication>
#include <QDebug>
#include <QUrl>

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

    QString testString("Hello World!");

    qDebug() << QUrl::toPercentEncoding(testString);

    return a.exec();
}

其實際輸出結果爲:

轉自:https://www.jianshu.com/p/f4908911e8d8

 

url中的中文字符,後期再討論

相關文章
相關標籤/搜索