0

在下面的代码中,我试图从远程服务器上的 php 脚本中获取数据,以响应一个变量中的发布请求。

我花了很多时间来寻找错误,但不能,这是在非 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等等。

4

2 回答 2

0

真是个傻瓜,我正在初始化 std::string response = "-1"; 因此 Server::writeString 中的每个“附加”都将结果附加到“-1”的末尾。需要确保如果有来自服务器的响应首先清除std::string。

于 2013-06-26T08:14:47.717 回答
0

也许你的附加位置是错误的。

替换你的写功能

((std::string*)stream)->append((char*)ptr,0,size*count);

遵循代码。

((std::string* )stream)->append((char*)ptr);
于 2013-06-26T07:33:55.933 回答