localhost에서 실행중인 Apache로 데이터를 보내려고합니다. 데이터는 JSON 형식으로 올바르게 형성,하지만 난 libcurl에를 사용하여 데이터를 보낼 때 PHP 웹 서버에 나는 그것은 임의의 문자를 반환C++ libcurl이 잘못된 데이터를 보내고 있습니다.
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("https://www.roblox.com/MobileAPI/Login");
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
어떤 아이디어 왜 무슨 인쇄되는입니까?
[EDITED]
는 쓰기 기능은 내가 지금
void Request::setPostData(std::string data) {
mData = data;
}
[수정 됨 2]
를 그래서 다음과 같이하고있는 데이터를 설정하고
size_t write(void *ptr, size_t size, size_t nmemb, std::string* data) {
data->append((char*)ptr, size * nmemb);
return size * nmemb;
}
아래로 보여 문제를 알고, 명백하게 libcurl은 char * 만 받아 들인다. 그리고 나는 std :: string을 보내고있다. 내 json 라이브러리가 json 객체를 std :: string으로 변환 한 이후로이 문제를 해결할 수있는 방법이 있습니까?
C- 스타일 문자열이 끝나지 않은 것 같습니다. 'setPostData'와'write' 함수를 보여주십시오. –
이것은 많은 정보가 누락되었습니다. 'write()'(libc'write()'는 curl의 write 함수와 완전히 다른 서명을 가지고 있기 때문에) 무엇입니까? 'mData' 란 무엇입니까? 'curl_easy_setopt()'는 varargs 함수이므로 타입 변환이 필수적으로 보인다. – dhke
좋아요, 요청에 따라 더 많은 정보를 업데이트했습니다. – Kurieita