libcurl发送格式错误的数据

C++ libcurl sending malformed data

本文关键字:数据 错误 格式 libcurl      更新时间:2023-10-16

我正在尝试将数据发送到运行在本地主机上的Apache。数据以json格式正确形成,但当我使用libcurl发送数据并在PHP web服务器上使用

回显字符串时
echo file_get_contents("php://input");

它返回随机字符,这是服务器回显的数据

`é    ²²²²▌▌▌▌4Ég▌ îHGáF

,在c++中,当我回显json时,它会正确打印。

Request.C + +

void Request::execute() {
auto curl = curl_easy_init();
if (curl) {
    curl_easy_setopt(curl, CURLOPT_URL, mUrl);
    curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
    curl_easy_setopt(curl, CURLOPT_USERAGENT, "RoBot/ Version 1");
    curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 50L);
    curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &mResponse);
    curl_easy_setopt(curl, CURLOPT_HEADERDATA, &mHeaders);
    curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &mResponseCode);
    if (!mData.empty()) {
        curl_easy_setopt(curl, CURLOPT_POST, true);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, mData);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, mData.size());
    }
    if (mHeadersData) {
        curl_easy_setopt(curl, CURLOPT_HEADER, true);
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, mHeadersData);
    }
    curl_easy_perform(curl);
    curl_easy_cleanup(curl);
    curl_slist_free_all(mHeadersData);
    curl = nullptr;
} 

我是这样执行它的

json data;
data["username"] = username;
data["password"] = password;
std::cout << data.dump() << std::endl;
Request* request = new Request("http://localhost:8080/Projects/");
request->setPostData(data.dump());
//request->addHeader("Content-Type: application/json");
request->execute();
std::cout << request->getResponse() << std::endl;
std::cout << request->getHeaders() << std::endl;

这是打印到控制台的内容

Username: id
Password: kf
{"password":"kf","username":"id"}
`é    ²²²²▌▌▌▌4Ég▌ îHGáF
HTTP/1.1 200 OK
Date: Sat, 27 Aug 2016 09:15:07 GMT
Server: Apache/2.4.6 (Win32) PHP/5.4.17
X-Powered-By: PHP/5.4.17
Content-Length: 36
Content-Type: text/html

知道为什么吗?

[编辑]

写函数如下所示

size_t write(void *ptr, size_t size, size_t nmemb, std::string* data) {
data->append((char*)ptr, size * nmemb);
return size * nmemb;
}

我正在设置数据做如下

void Request::setPostData(std::string data) {
mData = data;
}
[编辑2]

所以我现在知道问题了,显然libcurl只接受char*,而我正在发送std::string。是否有某种方式来解决这个问题,因为我的json库转换json对象std::string?

这一行不对:

curl_easy_setopt(curl, CURLOPT_POSTFIELDS, mData);

根据libcurl文档:

CURLOPT_POSTFIELDS解释

传递一个char *作为参数,指向HTTP POST操作中要发送的完整数据。您必须确保以您希望服务器接收数据的方式格式化数据。Libcurl不会以任何方式为您转换或编码它。例如,web服务器可能会假设这些数据是url编码的。

指向的数据不被库复制:因此,它必须由调用应用程序保存,直到相关的传输完成。可以通过设置CURLOPT_COPYPOSTFIELDS选项来改变这种行为(这样libcurl就会复制数据)。

您依赖于std::string的内部实现,以指向字符数据的char*指针开始,但这不能保证。这一行应该看起来像这样:

curl_easy_setopt(curl, CURLOPT_POSTFIELDS, mData.c_str());

保证您获得指向字符数据的char*指针。该指针将一直有效,直到std::string被修改或销毁。

也许尝试设置以下请求头?

接受:文本/平原
Accept-Charset: utf - 8
接受编码:身份

(也可能是Content-type,以确保服务器以正确的方式解释您发送的内容)

ref