Curl不会将多个数据发布到服务器

时间:2013-11-24 16:07:32

标签: c++ http post curl get

我正在编写一个工作应用程序,需要对服务器执行HTTP请求,并在JSON中获取响应。

目前,我的代码连接到服务器,并获得回复,这很棒。但是,我还需要将数据发送到服务器,服务器将处理它,并向我发送jSON。

我的问题是,由于POSTFIELDS,我能够发送一个“字段”,如果我插入更多的字段,我将无法收到任何响应。

代码如下:

// writefunc works, so there is no point adding its code in here
size_t Simulator::writefunc(void *ptr, size_t size, size_t nmemb, struct string *buffer_in);

void Simulator::move(Player *player)
{
    std::ostringstream ss_url;
    ss_url << API_URL << "functionCalled";
    char const *url = ss_url.str().c_str();

    std::ostringstream ss_postfields;

    // This will not work - And all values do NOT contain any space
    // The server do NOT receive any data (in POST and GET)
    ss_postfields  << "user_name=" << player->getName()
                        << "&user_secret=" << player->secret()
                            << "&app_id=" << player->getApp_id()
                                << "&lat=" << player->getLocation()->getLat()
                                    <<"&lon=" << player->getLocation()->getLon();

    // This will not work either
    // ss_postfields << "user_name=nicolas&app_id=2";

    // This works and will send the data to the server, which will receive and process it.
    // ss_postfields << "user_name=" << player->getName();

    const char *postfields = ss_postfields.str().c_str();

    CURL *curl_handle;
    curl_global_init(CURL_GLOBAL_ALL);
    curl_handle = curl_easy_init();

    if(curl_handle){
        struct string s;
        init_string(&s);

        curl_easy_setopt(curl_handle, CURLOPT_URL, url);
        curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, writefunc);
        curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
        curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &s);
        curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, postfields);
        curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDSIZE, strlen(postfields));
        curl_easy_setopt(curl_handle, CURLOPT_POST, 1L);
        CURLcode res = curl_easy_perform(curl_handle);

        if(CURLE_OK != res)
        {
            printf("Error: %s\n", strerror(res));
            exit(0);
        }

        printf("%s\n", s.ptr);
        curl_easy_cleanup(curl_handle);
        curl_global_cleanup();
    }
}

我认为当我只发送1个值或多个值时,我也会给出CURLOPT_HEADER和CURLOPT_VERBOSE的输出:

当我仅发送一个值时:

* About to connect() to localhost port 8888 (#0)
*   Trying ::1...
* connected
* Connected to localhost (::1) port 8888 (#0)
> POST [api_url]  HTTP/1.1
Host: localhost:8888
Accept: */*
Content-Length: 22
Content-Type: application/x-www-form-urlencoded

* upload completely sent off: 22 out of 22 bytes
< HTTP/1.1 200 OK
< Date: Sun, 24 Nov 2013 17:25:14 GMT
< Server: Apache
< X-Powered-By: PHP/5.3.20
< Cache-Control: no-cache
< X-Debug-Token: a41727
< Transfer-Encoding: chunked
< Content-Type: application/json
< 
* Connection #0 to host localhost left intact
0
HTTP/1.1 200 OK
Date: Sun, 24 Nov 2013 17:25:14 GMT
Server: Apache
X-Powered-By: PHP/5.3.20
Cache-Control: no-cache
X-Debug-Token: a41727
Transfer-Encoding: chunked
Content-Type: application/json

[ OUTPUT FROM SERVER HERE ]

* Closing connection #0

当我发送多个值时:

* About to connect() to localhost port 8888 (#0)
*   Trying ::1...
* connected
* Connected to localhost (::1) port 8888 (#0)
> POST [api_url] HTTP/1.1
Host: localhost:8888
Accept: */*
Content-Length: 1
Content-Type: application/x-www-form-urlencoded

* upload completely sent off: 1 out of 1 bytes
< HTTP/1.1 200 OK
< Date: Sun, 24 Nov 2013 17:30:13 GMT
< Server: Apache
< X-Powered-By: PHP/5.3.20
< Cache-Control: no-cache
< X-Debug-Token: d1947e
< Transfer-Encoding: chunked
< Content-Type: application/json
< 
* Connection #0 to host localhost left intact
0
HTTP/1.1 200 OK
Date: Sun, 24 Nov 2013 17:30:13 GMT
Server: Apache
X-Powered-By: PHP/5.3.20
Cache-Control: no-cache
X-Debug-Token: d1947e
Transfer-Encoding: chunked
Content-Type: application/json

[ OUTPUT FROM SERVER HERE ]

* Closing connection #0

2 个答案:

答案 0 :(得分:0)

好吧,你似乎错过了一个等号!

   ss_postfields  << "user_name=" << player->getName()
                    << "&user_secret=" << player->secret()
                        << "&app_id=" << player->getApp_id()
                            << "&lat=" << player->getLocation()->getLat()
                                <<"&lon=" << player->getLocation()->getLon();

而不是

   ss_postfields  << "user_name=" << player->getName()
                    << "&user_secret" << player->secret()
                        << "&app_id=" << player->getApp_id()
                            << "&lat=" << player->getLocation()->getLat()
                                <<"&lon=" << player->getLocation()->getLon();

答案 1 :(得分:0)

由于我匆忙,我使用HTTP套接字重写了所有内容。但我无法解决这个问题,所以我回到它,最后发现了问题。看起来cUrl并不喜欢溪流。

我想如果有人遇到同样的问题,我会更新这篇文章:

因此,以下内容不起作用:

std::ostringstream ss_postfields;

// This will not work - And all values do NOT contain any space
// The server do NOT receive any data (in POST and GET)
ss_postfields  << "user_name=" << player->getName()
                    << "&user_secret=" << player->getSecret();

const char *postfields = ss_postfields.str().c_str();
// ...
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, postfields);
CURLcode res = curl_easy_perform(curl_handle);

然而,奇怪的是,这将有效:

std::string str_postfields;
std::string user_name = player->getName();
std::string user_secret = player->getSecret();
std::string str_postfields = "user_name=" +user_name +"&user_secret=" +user_secret;

const char *postfields = str_postfields.c_str();
// ...
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, postfields);
CURLcode res = curl_easy_perform(curl_handle);