cURL / Mailing Lists / curl-library / Single Mail

curl-library

Cannot get anything but Error 52 - C++

From: Benjamin Strong <besostrong_at_gmail.com>
Date: Fri, 26 Feb 2010 08:13:08 -0600

Hello,

I am new to curl in C++, although I use it in PHP frequently.

For some reason, I cannot get anything but error 52, empty reply from server
error, regardless of the URL.

Here is my code.

#include "stdafx.h"

#include <iostream>

#include <string>
#include "stdio.h"
#include <curl.h>
using namespace std;

 // Write any errors in here
 static char errorBuffer[CURL_ERROR_SIZE];

 // 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;
 }

 // You know what this doe;s..
 void usage()
 {
   cout << "curltest: \n" ;
      cout << endl;
   cout << " Usage: curltest url\n";
     cout << endl;
 }

 /*
  * The old favorite
  */
 int main(int argc, char* argv[])
 {

   if ( 1)
   {
       string url("http://safemart.com");

     cout << "Retrieving " << url << endl;

     // Our curl objects
     CURL *curl;
     CURLcode result;

     // Create our curl handle
     curl = curl_easy_init();
     if (curl)
     {
       // Now set up all of the curl options
       curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
       curl_easy_setopt(curl, CURLOPT_URL, "http://safemart.com");
       curl_easy_setopt(curl, CURLOPT_HEADER, 1);
       curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
       curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);

       curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
       curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
       curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U;
Windows NT 6.0; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR
3.5.30729)");

       // Attempt to retrieve the remote page
       result = curl_easy_perform(curl);

       // Always cleanup
       curl_easy_cleanup(curl);

       // Did we succeed?
       if (result == CURLE_OK)
       {
         cout << buffer << "\n";
         system("pause");
         return 10;
       }
       else
       {
         cout << "Error: [" << result << "] - " << errorBuffer << endl;
         system("pause");
         return -1;
       }
     }
  }
 }

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2010-02-26