【问题标题】:C - URL encodingC - URL 编码
【发布时间】:2011-04-30 14:45:02
【问题描述】:

有没有简单的方法在 C 中进行 URL 编码?我正在使用 libcurl,但我还没有找到方法。具体来说,我需要进行百分比转义。

【问题讨论】:

    标签: c urlencode libcurl


    【解决方案1】:

    基于 wikipedia 的 C 语言,无需分配和释放。确保输出缓冲区至少是输入 URL 字符串的 3X。通常你只需要编码最多 4K,因为 URL 往往很短,所以只需在堆栈上进行。

    char rfc3986[256] = {0};
    char html5[256] = {0};
    
    void url_encoder_rfc_tables_init(){
    
        int i;
    
        for (i = 0; i < 256; i++){
    
            rfc3986[i] = isalnum( i) || i == '~' || i == '-' || i == '.' || i == '_' ? i : 0;
            html5[i] = isalnum( i) || i == '*' || i == '-' || i == '.' || i == '_' ? i : (i == ' ') ? '+' : 0;
        }
    }
    
    char *url_encode( char *table, unsigned char *s, char *enc){
    
        for (; *s; s++){
    
            if (table[*s]) *enc = table[*s];
            else sprintf( enc, "%%%02X", *s);
            while (*++enc);
        }
    
        return( enc);
    }
    

    这样使用

    url_encoder_rfc_tables_init();
    
    url_encode( html5, url, url_encoded);
    

    【讨论】:

    • 这一行 "sprintf( enc, "%c", table[*s])" 可以简化为 "*enc = *s" - 你可以避免调用昂贵的格式化函数。
    • 你的意思是 *enc = table[*s]?
    【解决方案2】:

    curl_escape

    显然已被

    取代

    curl_easy_escape

    【讨论】:

    • 注意!上述函数不会产生与(例如)PHP urlencode 函数兼容的结果
    【解决方案3】:

    我写这个也是为了处理空格字符的查询字符串编码

    用法:UrlEncode("http://www.example.com/index.html?Hello=World", ":/", buffer, buf_size)

    url:要编码的 url 字符串。可以是字符串字面量或字符串数​​组

    encode:要编码的以零结尾的字符串。这很好,因为您可以在运行时确定要编码多少 url

    缓冲区:保存新字符串的缓冲区

    大小:缓冲区的大小

    return:如果缓冲区足够大,则返回新字符串的大小;如果缓冲区不够大,则返回所需的缓冲区大小。如果要分配所需的确切大小,可以双击此功能。

        int UrlEncode(char* url, char* encode,  char* buffer, unsigned int size)
        {
            char chars[127] = {0};
            unsigned int length = 0;
    
            if(!url || !encode || !buffer) return 0;
    
    //Create an array to hold ascii chars, loop through encode string
    //and assign to place in array. I used this construct instead of a large if statement for speed.
            while(*encode) chars[*encode++] = *encode;
    
    //Loop through url, if we find an encode char, replace with % and add hex
    //as ascii chars. Move buffer up by 2 and track the length needed.
    //If we reach the query string (?), move to query string encoding
        URLENCODE_BASE_URL:
            while(size && (*buffer = *url)) {
                if(*url == '?') goto URLENCODE_QUERY_STRING;
                if(chars[*url] && size > 2) {
                    *buffer++ = '%';
                    itoa(*url, buffer, 16);
                    buffer++; size-=2; length+=2;
                }
                url++, buffer++, size--; length++;  
            }
            goto URLENCODE_RETURN;
    
    //Same as above but on spaces (' '), replace with plus ('+') and convert
    //to hex ascii. I moved this out into a separate loop for speed.
        URLENCODE_QUERY_STRING:
            while(size && (*buffer = *url)) {
                if(chars[*url] && size > 2) {
                    *buffer++ = '%';
                    if(*url == ' ') itoa('+', buffer, 16);
                    else itoa(*url, buffer, 16);
                    buffer++; size-=2; length+=2;
                }
                url++, buffer++, size--; length++;
            }
    
    //Terminate the end of the buffer, and if the buffer wasn't large enough
    //calc the rest of the url length and return
        URLENCODE_RETURN:
            *buffer = '\0';
            if(*url)
            while(*url) { if(chars[*url]) length+=2; url++; length++; }
            return length;
        }
    

    这个函数几乎可以处理您需要的大多数(如果不是全部)url 编码。最重要的是 - 它真的很快!

    【讨论】:

      猜你喜欢
      • 2011-09-20
      • 2012-06-02
      • 2011-12-26
      • 2019-06-01
      • 1970-01-01
      • 2012-03-28
      • 2014-02-19
      相关资源
      最近更新 更多