简单明了:我正在从数据库生成一个整数列表,我需要将其上传到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,... etc”

最佳答案

我自己解决了问题,但问题不在于CURL,而在于流参数,尽管打印内容没有显示出要插入的任何空间。

09-07 05:23