在下面的代码中,我试图从远程服务器上的php脚本获取数据,以响应变量中的post请求。
我花了这么多时间找到bug,但是不能,这是在非oop环境中完美运行的相同代码。但现在它返回所有类型的错误数据,返回的数据(std :: string)是虽然持久,但对于一组发布的数据,返回的数据(std :: string)总是相同的。
我用html格式检查了脚本,没有问题。所以我猜我写代码有问题。
我在OOP环境中使用C ++和libCurl。
#include "Server.h"
namespace model
{
// private:
Server::Server ( )
{
curl_global_init ( CURL_GLOBAL_ALL );
curl = curl_easy_init ( ) ;
};
Server* Server::singleton = NULL;
// public:
Server* Server::Instance ( )
{
if ( !singleton ) singleton = new Server ( );
return singleton;
};
Server::~Server ( )
{
curl_easy_cleanup ( curl );
curl_global_cleanup ( );
};
std::string Server::readScript ( std::string scriptAddress, std::string postData )
{
std::string response = "-1";
if ( curl )
{
curl_easy_setopt ( curl, CURLOPT_URL, scriptAddress.c_str ( ) );
curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, &Server::writeString );
curl_easy_setopt ( curl, CURLOPT_WRITEDATA, &response );
if ( ! postData.empty ( ) )
{
curl_easy_setopt ( curl, CURLOPT_POSTFIELDS, postData.c_str ( ) );
}
curl_easy_setopt ( curl, CURLOPT_CONNECTTIMEOUT, 10L );
CURLcode cc = curl_easy_perform ( curl );
if ( cc == CURLE_OPERATION_TIMEDOUT ) MessageBox ( 0, "The operation timed out.\nPlease try again later.", "Error!", MB_OK | MB_ICONEXCLAMATION );
else if ( cc == CURLE_COULDNT_CONNECT ) MessageBox ( 0, "Couldn't conect to the server.\nPlease try again later.", "Error!", MB_OK | MB_ICONEXCLAMATION );
else if ( cc == CURLE_REMOTE_ACCESS_DENIED ) MessageBox ( 0, "Access is denied.\nPlease contact our support team.", "Error!", MB_OK | MB_ICONEXCLAMATION );
else if ( cc == CURLE_GOT_NOTHING ) MessageBox ( 0, "Server did not return anything.\nPlease try again later, if the problem persist please contact our support team.", "Error!", MB_OK | MB_ICONEXCLAMATION );
else if ( cc == CURLE_REMOTE_FILE_NOT_FOUND ) MessageBox ( 0, "Couldn't find resource.\nPlease try again later, if the problem persist please contact our support team.", "Error!", MB_OK | MB_ICONEXCLAMATION );
else if ( cc == CURLE_FAILED_INIT ) MessageBox ( 0, "The initialization failed.\nPlease contact our support team.", "Error!", MB_OK | MB_ICONEXCLAMATION );
else if ( cc == CURLE_COULDNT_RESOLVE_HOST ) MessageBox ( 0, "Couldn't resolve host.\nPlease contact our support team.", "Error!", MB_OK | MB_ICONEXCLAMATION );
else if ( cc == CURLE_WRITE_ERROR ) MessageBox ( 0, "Response write error.\nPlease contact our support team.", "Error!", MB_OK | MB_ICONEXCLAMATION );
else if ( cc == CURLE_TOO_MANY_REDIRECTS ) MessageBox ( 0, "Droping connection, due to too many redirects.\nPlease contact our support team.", "Error!", MB_OK | MB_ICONEXCLAMATION );
else if ( cc == CURLE_SEND_ERROR ) MessageBox ( 0, "Failed sending data to the server.\nPlease try again later, if the problem persist please contact our support team.", "Error!", MB_OK | MB_ICONEXCLAMATION );
else if ( cc == CURLE_REMOTE_FILE_NOT_FOUND ) MessageBox ( 0, "Remote server could not be found.\nPlease try again later, if the problem persist please contact our support team.", "Error!", MB_OK | MB_ICONEXCLAMATION );
MessageBox( 0, response.c_str(), "response", 0 );
}
else
{
MessageBox ( 0, "Something went wrong.\nPlease try again later.", "Error!", MB_OK | MB_ICONEXCLAMATION );
}
return response;
};
/** private static function */
/** -------------------------------------------------------------------------------------------------------------------------------- */
/** -------------------------------------------------------------------------------------------------------------------------------- */
int Server::writeString ( void *ptr, int size, int count, void *stream )
{
( ( std::string* ) stream )->append ( ( char* ) ptr, 0, size*count );
return size*count;
}
};// end of namespace model
修改 当我期待 0 时,它会返回 -10 ,当 3 时,它会返回 -13 ,当 1 预计会返回 -11 ,依此类推。
答案 0 :(得分:0)
也许你的追加位置是错误的。
替换你的写功能
((std::string*)stream)->append((char*)ptr,0,size*count);
遵循代码。
((std::string* )stream)->append((char*)ptr);
答案 1 :(得分:0)
这样的傻瓜,我正在初始化std :: string response =“ - 1”;所以Server :: writeString中的每个“append”都将结果附加在“-1”的末尾。需要确保如果服务器有响应,请先清除std :: string。