zoukankan      html  css  js  c++  java
  • 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编码值

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

    URL特殊字符转义,URL中一些字符的特殊含义,基本编码规则如下:

    1、空格换成加号(+)
    2、正斜杠(/)分隔目录和子目录
    3、问号(?)分隔URL和查询
    4、百分号(%)制定特殊字符
    5、#号指定书签
    6、&号分隔参数

    如果需要在URL中用到,需要将这些特殊字符换成相应的十六进制的值
    +     %2B
    /      %2F
    ?     %3F
    %    %25
    #     %23
    &    %26

    Qt 中使用 QUrl 对字符串进行 URL 格式化编码

    QUrl 为我们提供了很多的便利方法,其中对字符串进行 URL 格式化编码的方法

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

    就是一个很方便的方法,在这个方法中,我们可以简单地对字符串进行编码,也可以通过指定第二个参数 exclude 指定哪些字符不需要编码,以及指定第三个参数 include 强制将某些字符进行编码。

    下面是 Qt 文档中的一个实例:

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

    下面是一个简单的使用实例:

    #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中的中文字符,后期再讨论

  • 相关阅读:
    匿名,排序,过滤,映射,递归函数
    内置函数图
    for(var i in items) 和 for(var i;i<items.length;i++) 区别
    js中var、let、const的区别 (待总结)
    eclipse拉取git项目 Read timed out after 30,000 ms
    eclispe git config配置文件配置远程仓库
    git pull出错:cannot pull into a repository with state: merging_resolved"
    HttpClient之用CloseableHttpClient发送post请求
    注意设置httpclient连接数
    This compilation unit is not on the build path of java project (此编译单元不在java项目的生成路径上)
  • 原文地址:https://www.cnblogs.com/liushui-sky/p/10892097.html
Copyright © 2011-2022 走看看