當我使用cURL獲取網頁時,它似乎沒有以正確的格式編碼或接收數據。還是cURL以標準格式獲取網頁並且應用程序需要解壓縮/放縮/編碼輸出?Cpp:在cURL中使用UTF-8
其中一個網頁我一直在試圖解決將例如做的:
- LET'S GET荒謬的 - 雷度福
- 的轟鳴聲 - 凱蒂·佩裏
- 破壞球€「麥莉·賽勒斯
- 談話骯髒Jason Derulo壯舉。 2 Chainz
在我的瀏覽器中,上述示例在設置爲「Western ISO-8859-1」編碼時顯示此項。在UTF-8上時,它可以正常工作。我的應用程序中出現相同的情況。除了這個之外,它對每個短跑都很好。
又如維基UTF-8的文章:http://en.wikipedia.org/wiki/UTF-8
應用程序的輸出是如下(需要10代表後圖像): http://img21.imageshack.us/img21/7048/i23c.png
我的應用程序利用標準捲曲禁止複製到內存的例子有用於測試的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代碼頁)。 –
感謝您的澄清。雖然任何想法如何組合:「從捲曲獲得:?ffffffe2,? - ffffff80,」 - ffffff93。可以用UTF-8創建一個破折號( - )? – Larry
正確地顯示UTF-8(或許多其他編碼)幾乎與藝術接近,並且在不同的操作系統之間明顯不同。 IOW,對不起,但我真的幫不了多少忙。 –