cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: curl_easy_escape question

From: <Surfman19_at_gmx.at>
Date: Wed, 19 Apr 2006 01:36:56 +0200 (MEST)

hi,
i tried the following code, but i did get a response from the server here
into the std::string buffer why not?? buffer is empty??? why? greets

// Write all expected data in here
static string buffer;
// This is the writer call back function used by curl
static int writer(char *data, size_t size, size_t nmemb, std::string
*buffer)
{
  // What we will return
  int result = 0;

  // Is there anything in the buffer?
  if (buffer != NULL)
  {
    // Append the data to the buffer
    buffer->append(data, size * nmemb);

    // How much did we write?
    result = size * nmemb;
  }

  return result;
}
std::string url_encode(const std::string& str)
{
    char* tmp = curl_escape(str.c_str(), str.size());
    if(!tmp)
        throw std::bad_alloc();
    std::string encoded(tmp);
    curl_free(tmp);
    return encoded;
}

void replace_all(std::string& str, const std::string& search, const
std::string& replace)
{
    std::string::size_type diff_len = replace.length()-search.length()+1;

    for(std::string::size_type i=str.find(search); std::string::npos != i;
i=str.find(search, i+diff_len))
    {
        str.replace(i, search.length(), replace);
    }
}

int main(void)
{
  CURL *curl;
  CURLcode res;
  string text;
  string textencoded;
  string postdata;
  text = "Hallöchen 1234 curltest";
  textencoded = url_encode(text);
  replace_all(textencoded, "%20", "+"); // replace ' ' with '+'

  curl = curl_easy_init();
  if(curl) {
    /* First set the URL that is about to receive our POST. This URL can
       just as well be a https:// URL if that is what should receive the
       data. */
    curl_easy_setopt(curl, CURLOPT_URL,
"http://www.rafb.net/paste/paste.php");
    /* Now specify the POST data */
        postdata = "lang=C%2B%2B&nick=&desc=&cvt_tabs=No&text=";
        postdata += textencoded;
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postdata.c_str());
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);

    /* Perform the request, res will get the return code */
    res = curl_easy_perform(curl);

        if(res!=CURLE_OK) {
                fprintf(stderr,"Error: %s\n",curl_easy_strerror(res));
                curl_easy_cleanup(curl);
                return -1;
        }

        cout << buffer.c_str() << endl; <----- empty...why???

    /* always cleanup */
    curl_easy_cleanup(curl);
  }
  cin.get();
  return 0;
}

-- 
"Feel free" - 10 GB Mailbox, 100 FreeSMS/Monat ...
Jetzt GMX TopMail testen: http://www.gmx.net/de/go/topmail
Received on 2006-04-19