libcurl发送格式错误的数据
C++ libcurl sending malformed data
我正在尝试将数据发送到运行在本地主机上的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
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 尝试通过OCI例程从Oracle获取blob数据,但出现错误:ORA-01008:并非所有变量都绑定
- 使用 char* 存储数据和产生的错误
- Static_cast转换为错误的数据类型,但结果仍然正确?
- 创建服务失败0x6f7存根收到错误数据
- 通过 host() 从 af::array 检索数据会导致错误的数据
- OpenCV C++ 3 维垫数据访问错误值
- 错误: 无效使用非静态数据成员"应用程序::应用程序构造函数"
- 错误 - 自定义数据类型作为有效负载,带有提升::几何
- 类方法 - 数据结构中 For 循环的运行时错误
- 从文件中读取的数据,但发生核心转储错误
- 如果在 C++ 构造函数中以错误的顺序初始化对象数据,会发生什么类型的错误
- 删除使数据处于"错误状态"的默认构造函数的模式?
- 服务器客户端通过原始数据错误C 发送接收结构
- 对象无法访问其私人数据.错误:在这种情况下私有
- SVM训练数据错误
- 库单元测试的覆盖率数据错误
- Unix套接字发送接收客户端到服务器大块数据错误
- 无法通过windows UDP套接字发送数据:错误代码10035
- 着色器存储缓冲区发送的数据错误