c++ - CPP : Using UTF-8 in cURL

标签 c++ visual-studio-2010 curl console-application

当我使用 cURL 获取网页时,它似乎没有以正确的格式编码或接收数据。或者是 cURL 获取标准格式的网页并且应用程序必须解压缩/缩小/编码输出?

例如,我一直在尝试处理的网页之一:

  • 让我们开始搞笑吧 - Redfoo
  • 咆哮 - 凯蒂·佩里
  • WRECKING BALL €“麦莉赛勒斯
  • TALK DIRTY Jason Derulo 壮举。 2 链子

在我的浏览器中,上面的示例在设置为“Western ISO-8859-1”编码时显示此内容。在 UTF-8 上,它可以像破折号一样正常工作。同样的场景出现在我的应用程序中。它适用于除此之外的所有破折号。

另一个例子是 wiki UTF-8 文章:http://en.wikipedia.org/wiki/UTF-8

应用程序的输出如下(需要 10 个 rep 才能发布图像): http://img21.imageshack.us/img21/7048/i23c.png

我的应用程序使用标准的 cURL 复制到内存示例并进行了一些修改以测试 wiki 链接:

代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>

#include <curl/curl.h>

struct MemoryStruct {
    char *memory;
    size_t size;
};


static size_t
    WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
    size_t realsize = size * nmemb;
    struct MemoryStruct *mem = (struct MemoryStruct *)userp;

    mem->memory = (char*)realloc(mem->memory, mem->size + realsize + 1);
    if(mem->memory == NULL) {
        /* out of memory! */ 
        printf("not enough memory (realloc returned NULL)\n");
        return 0;
    }

    memcpy(&(mem->memory[mem->size]), contents, realsize);
    mem->size += realsize;
    mem->memory[mem->size] = 0;

    return realsize;
}


int main(void)
{
    CURL *curl_handle;
    CURLcode res;

    struct MemoryStruct chunk;

    chunk.memory = (char*)malloc(1);  /* will be grown as needed by the realloc above */ 
    chunk.size = 0;    /* no data at this point */ 

    curl_global_init(CURL_GLOBAL_ALL);

    /* init the curl session */ 
    curl_handle = curl_easy_init();

    /* specify URL to get */ 
    curl_easy_setopt(curl_handle, CURLOPT_URL, "http://en.wikipedia.org/wiki/UTF-8");

    /* send all data to this function  */ 
    curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);

    /* we pass our 'chunk' struct to the callback function */ 
    curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);

    /* some servers don't like requests that are made without a user-agent
    field, so we provide one */ 
    curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");

    /* get it! */ 
    res = curl_easy_perform(curl_handle);

    /* check for errors */ 
    if(res != CURLE_OK) {
        fprintf(stderr, "curl_easy_perform() failed: %s\n",
            curl_easy_strerror(res));
    }
    else
    {
        char * x = chunk.memory;
        bool copyFlag = false;
        bool waitFlag = false;

        for(unsigned i = 0;i<chunk.size;i++)
        {
            if (copyFlag == true)
            {
                if (waitFlag == false)
                {
                    if (*x == '>')
                        waitFlag = true;
                }
                else if (waitFlag == true)
                {
                    if (*x == '<')
                    {
                        waitFlag = false;
                        copyFlag = false;
                        std::cout << std::endl;
                    }
                    else
                        std::cout<< *x;
                }
            }
            else if (*(x+5) == '<' && *(x+6) == '/' && *(x+7) == 'a' && *(x+8) == '>' && *(x+9) == '<' && *(x+10) == '/' && *(x+11) == 't' && *(x+12) == 'd' && *(x+13) == '>')//</a></td>
            {
                copyFlag = true;
            }
            x++;
        }
    }

    /* cleanup curl stuff */ 
    curl_easy_cleanup(curl_handle);

    if(chunk.memory)
        free(chunk.memory);

    /* we're done with libcurl, so clean it up */ 
    curl_global_cleanup();
    system("Pause");
    return 0;
}

我可以看到控制台和调试器之间的区别,它们每个都只能解释某些字符集,但是否可以在 cURL 中进行不同的设置,或者我应该编写一个编码函数?如果是这样,我应该如何开始?

最佳答案

cURL 返回源的原始字符格式。在这种情况下,以 UTF-8 编码出现的破折号返回为 3 个字节,“â - ffffffe2”“€ - ffffff80,”“- ffffff93”。根据 Unicode Index,此破折号表示为“0xE2 0x80 0x94”或“e28094”。

示例:在页面抓取和手动遍历字符时,始终可以测试字符是否大于或等于 0xFFFF00。

    if (*x => 0xFFFFFF00)
        if (UTF_Conv(x))
        {
            //do something with the character?
            x=+2;
        }

接着是:

int UTF_Conv(char x)
{
    if ((*x == 0xffffffe2) && (*(x+1) == 0xffffffe2) && (*(x+2) == 0xffffffe2)) //dash convert
    {
        *(x+2) = '-';
        return 1;
    }

}

用破折号替换第三个字符,该破折号通常在整个应用程序的其余部分进行测试,而不是晦涩难懂的破折号,并将抓取索引 x 增加到可用的破折号。

关于c++ - CPP : Using UTF-8 in cURL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19394050/

相关文章:

c++ - 在修改 <some_container> 的大小时迭代 std::vector<some_container> 安全吗?

python - python 请求中的 curl 语句 `--form input=@./thefile.pdf`

c++ - 如何使用 unique_ptr 实现零规则

c++ - WSL (Ubuntu) 与 X11、glfw(3) 编程兼容吗?

c++ - 为什么 Qt 在头文件中包含现有类的空类定义?

c# - 我如何知道何时/什么正在加载某些程序集?

visual-studio-2010 - 如何让 IntelliSense 在 Razor 助手的脚本标记内工作?

c++ - 为什么在从控制台编译后取消引用 string::iterator 不会在 .end() 处引发错误?

bash - 为什么我的 Curl 命令大部分时间都无法下载文件,但有时却可以?

php - 我自己的站点的 file_get_contents() 连接被拒绝