cURL を使用して Web ページを取得すると、データが正しい形式でエンコードまたは受信されないようです。それとも、cURL が Web ページを標準形式で取得し、アプリケーションが出力を圧縮解除/圧縮/エンコードする必要があるのでしょうか?
私が対処しようとしてきたWebページの1つは、たとえば次のようになります。
- ばかげてみましょう - Redfoo
- 咆哮 - ケイティ・ペリー
- レッキング・ボール - マイリー・サイラス
- TALK DIRTY ジェイソン・デルーロ Feat. 2チェインズ
私のブラウザでは、「Western ISO-8859-1」エンコーディングに設定すると、上記の例が表示されます。UTF-8 の場合、ダッシュとして正常に機能します。私のアプリケーションにも同じシナリオが表示されます。これを除くすべてのダッシュで正常に機能します。
別の例は、wiki の UTF-8 記事です: http://en.wikipedia.org/wiki/UTF-8
アプリケーションの出力は次のとおりです (画像を投稿するには 10 人の担当者が必要です): http://img21.imageshack.us/img21/7048/i23c.png
私のアプリケーションでは、wiki リンクをテストするためにいくつかの変更を加えた、標準の cURL のメモリへのコピーの例を利用しています。
コード:
#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 でこれを別の方法で設定することは可能ですか、それともエンコーディング関数を作成する必要がありますか? もしそうなら、私はどのように始めるべきですか?