cURL / Mailing Lists / curl-library / Single Mail

curl-library

RE: Unhandled exception whenusingcurl_easy_perform()inVisualStudio2005 on Windows XP.

From: Xu Xiaogang-a19881 <nickxu_at_motorola.com>
Date: Wed, 4 Jun 2008 16:30:44 +0800

Attach my code and the screen here, for your reference. The exception happens when curl_easy_perform is called.

Please help to figure out anything wrong in my code.

#include <string>
#include <sstream>

#include <curl/curl.h>

using namespace std;

class CurlUtil
{
public:
        CurlUtil()
        {
                curl_global_init(CURL_GLOBAL_ALL);
                curl = curl_easy_init();
                use_proxy = false;
        }
        
        ~CurlUtil()
        {
                curl_easy_cleanup(curl);
                curl_global_cleanup();
        }

        static size_t curlWriteCallback(void *ptr, size_t size, size_t nmemb, void *userp);
        static size_t curlReadCallback( void *ptr, size_t size, size_t nmemb, void *stream);

        CURLcode setUseProxy(bool use_proxy)
        {
                this->use_proxy = use_proxy;

                return CURLE_OK;
        }

        CURLcode setHttpProxy(string http_proxy)
        {
                this->http_proxy = http_proxy;

                return CURLE_OK;
        }

        CURLcode setProxyAuthentification(string user_name, string password)
        {
                this->proxy_authentification = user_name+":"+password;

                return CURLE_OK;
        }

        CURLcode setUrl(string url_string)
        {
                CURLcode errornum;
                errornum = curl_easy_setopt(curl, CURLOPT_URL, url_string.c_str());
                if (errornum != CURLE_OK)
                {
                        throw curl_easy_strerror(errornum);
                }
                
                return CURLE_OK;
        }

        CURLcode setReferer(string referer_string)
        {
                CURLcode errornum;
                errornum = curl_easy_setopt(curl, CURLOPT_REFERER, referer_string.c_str());

                if (errornum != CURLE_OK)
                {
                        throw curl_easy_strerror(errornum);
                }

                return CURLE_OK;
        }

        CURLcode setPost(bool postornot)
        {
                CURLcode errornum;
                errornum = curl_easy_setopt(curl, CURLOPT_POST, postornot);

                if (errornum != CURLE_OK)
                {
                        throw curl_easy_strerror(errornum);
                }

                return CURLE_OK;
        }

        CURLcode setPostField(string post_content)
        {
                CURLcode errornum;
                errornum = curl_easy_setopt (curl, CURLOPT_POSTFIELDS, post_content.c_str());

                if (errornum != CURLE_OK)
                {
                        throw curl_easy_strerror(errornum);
                }

                return CURLE_OK;
        }

        CURLcode setUserAgent(const char* user_agent)
        {
                CURLcode errornum;
                errornum = curl_easy_setopt (curl, CURLOPT_USERAGENT, user_agent);

                if (errornum != CURLE_OK)
                {
                        throw curl_easy_strerror(errornum);
                }

                return CURLE_OK;
        }

        CURLcode setCookieFile(const char* cookie_file)
        {
                CURLcode errornum;
                errornum = curl_easy_setopt (curl, CURLOPT_COOKIEFILE, cookie_file);

                if (errornum != CURLE_OK)
                {
                        throw curl_easy_strerror(errornum);
                }

                return CURLE_OK;
        }

        CURLcode setGet(bool getornot)
        {
                CURLcode errornum;

                errornum = curl_easy_setopt (curl, CURLOPT_HTTPGET, getornot);
                if (errornum != CURLE_OK)
                {
                        throw curl_easy_strerror(errornum);
                }

                return CURLE_OK;
        }

        CURLcode setPut(bool putornot)
        {
                CURLcode errornum;

                errornum = curl_easy_setopt (curl, CURLOPT_PUT, putornot);
                if (errornum != CURLE_OK)
                {
                        throw curl_easy_strerror(errornum);
                }

                return CURLE_OK;
        }

        CURLcode setPutContent(const istringstream &put_content)
        {
                curl_data.str(put_content.str());

                return CURLE_OK;
        }

        string getContent(string url)
        {
                CURLcode errornum;

                setUrl(url);
                setGet(true);
                if ((errornum = executeCurl(this->curl)) != CURLE_OK)
                {
                        throw curl_easy_strerror(errornum);
                }
                return curl_result;
        }

        string postContent(string url, string post_content)
        {
                CURLcode errornum;

                setUrl(url);
                setPost(true);
                setPostField(post_content);
                setUserAgent("Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.8.1.14) Gecko/20080404 (CK-FoxPlus) Firefox/2.0.0.14");

                if ((errornum = executeCurl(this->curl)) != CURLE_OK)
                {
                        throw curl_easy_strerror(errornum);
                }
                return curl_result;
        }
        
        string putContent(string url, string put_content)
        {
                CURLcode errornum;

                setUrl(url);
                setPut(true);
                istringstream mystream(put_content);
                setPutContent(mystream);

                errornum = curl_easy_setopt(curl, CURLOPT_READFUNCTION, CurlUtil::curlReadCallback);
                if (errornum != CURLE_OK)
                {
                        throw curl_easy_strerror(errornum);
                }

            errornum = curl_easy_setopt(curl, CURLOPT_READDATA, &curl_data);
                if (errornum != CURLE_OK)
                {
                        throw curl_easy_strerror(errornum);
                }

                if ((errornum = executeCurl(this->curl)) != CURLE_OK)
                {
                        throw curl_easy_strerror(errornum);
                }
                return curl_result;
        }

        string executeCurl()
        {
                CURLcode errornum;

                if ((errornum = curl_easy_perform(curl)) != CURLE_OK)
                {
                        throw curl_easy_strerror(errornum);
                }

                return curl_result;
        }

        /* misc support */
        CURLcode initCurl();

protected:

private:

public:

protected:

private:
        bool use_proxy;
        string http_proxy;
        string proxy_authentification;
        
        string curl_result;
        stringstream curl_data;
        CURL* curl;
};

Regards
-Xiaogang

-----Original Message-----
From: curl-library-bounces_at_cool.haxx.se [mailto:curl-library-bounces_at_cool.haxx.se] On Behalf Of Daniel Stenberg
Sent: 2008Äê6ÔÂ4ÈÕ 15:45
To: libcurl development
Subject: RE: Unhandled exception whenusingcurl_easy_perform()inVisualStudio2005 on Windows XP.

On Wed, 4 Jun 2008, Xu Xiaogang-a19881 wrote:

> When using this function pure in C, and build with Visual Studio 2005
> on Windows XP, it is OK. Everything can run correctly. But, If using
> this function in C++, the exception will happen.
>
> The same set of C++ code using libcurl in Cygwin, it is also OK.
>
> Anybody has any idea about it?

Please don't top-post.

And if you want our help, you need to spell it out. Show us exactly what you do that doesn't work.

-- 
  / daniel.haxx.se

exception.JPG
Received on 2008-06-04