1

简单明了:我正在从数据库中生成一个整数列表,我需要将它上传到 Pastebin.com。这是我的代码:

curl_global_init(CURL_GLOBAL_ALL);
if (CURL* curl = curl_easy_init()) {
    std::ostringstream postField;
    postField << "api_user_key=" << "&" << "api_option=paste" << "&" << "api_paste_private=1" << "&";
    postField << "api_paste_name=GuidSet" << "&" << "api_paste_expire_date=10M" << "&";
    postField << "api_paste_format=text" << "&" << "api_dev_key=XXXXXXXXXXXXXXXX" << "&" << "api_paste_code=";
    for (std::set<uint32>::const_iterator itr = resultSet.begin(); itr != resultSet.end(); ++itr) {
        postField << *itr;
        if (itr != --resultSet.end())
            postField << ",";
    }

    struct MemoryStruct chunk;
    chunk.memory = (char*)malloc(1);
    chunk.size   = 1;

    curl_easy_setopt(curl, CURLOPT_URL, "http://pastebin.com/api/api_post.php");
    curl_easy_setopt(curl, CURLOPT_POST, 1);
    char* postStr = curl_easy_escape(curl, postField.str().c_str(), postField.str().length());
    printf("POST parameters: %s\n", postStr);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postStr);
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
    curl_easy_setopt(curl, CURLOPT_NOBODY, 0);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&chunk);
    curl_easy_setopt(curl, CURLOPT_USERAGENT, "TrinityCore GuidBot/1.0");

    curl_easy_perform(curl);
    curl_easy_cleanup(curl);

    self->_parentThread.sendCommand("PRIVMSG %s :%s: Finished, data available at %s",
        searchParams.targetRoom.c_str(), searchParams.actionInvoker.c_str(), chunk.memory);

    if (chunk.memory)
        free(chunk.memory);
    curl_free(postStr);
}
curl_global_cleanup();

但是,在执行该代码时,我总是得到这个:

* About to connect() to pastebin.com port 80 (#0)
*   Trying 72.20.36.113...
* connected
* Connected to pastebin.com (72.20.36.113) port 80 (#0)
> POST /api/api_post.php HTTP/1.1
User-Agent: TrinityCore GuidBot/1.0
Host: pastebin.com
Accept: */*
Content-Length: 609
Content-Type: application/x-www-form-urlencoded

* upload completely sent off: 609 out of 609 bytes
< HTTP/1.1 200 OK
< Server: nginx
< Date: Sun, 28 Oct 2012 11:21:33 GMT
< Content-Type: text/html
< Transfer-Encoding: chunked
< Connection: keep-alive
< Vary: Accept-Encoding
<
* Connection #0 to host pastebin.com left intact
* Closing connection #0
Bad API request, invalid api_option

有什么我明显做错了吗?应该粘贴的块必须看起来像“48,50,59,...等”

4

1 回答 1

0

自己解决了,问题不在于 CURL,而是在流参数中,虽然 printf'ing 的东西没有显示任何插入的空间。

于 2012-10-29T11:17:02.767 回答