Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7771369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:38:08+00:00 2026-06-01T16:38:08+00:00

I’m having an issue with an HTTP class I’ve gotten (and modified from) http://www.zedwood.com/article/125/cpp-libcurl-static-class

  • 0

I’m having an issue with an HTTP class I’ve gotten (and modified from) http://www.zedwood.com/article/125/cpp-libcurl-static-class

Http.h

#ifndef HTTP_H
#define HTTP_H

#include <curl/curl.h>
#include <string>
#include <map>

using namespace std;

class Http {

    public:
        Http(){};
        ~Http(){};

        static string get(const string &url);
        static string get(const string &url, string referer);

        static string post(const string &url, map<string, string> params);
        static string post(const string &url, map<string, string> params, string referer);

        static string get_current_url();
        static string get_last_url();

        static void set_proxy(string ip, long port);
        static void clear_proxy();

        static void set_remote_port(long port);

    private:
        static string request(const string &url, string referer, bool post, const string &params);

        static int writer(char *data, size_t size, size_t nmemb, string *buffer);

        static string urlencode(const string &str);
        static string dechex(char dec);
        static string build_param_string(map<string, string> params);

        static string last_url;
        static string current_url;

        static bool use_proxy;
        static string proxy_ip;
        static long proxy_port;

        static double total_to_download;
        static double now_downloaded;

        static long remote_port;

        static string buffer;
};

#endif

Http.cpp

#include "Http.h"

using namespace std;

string Http::last_url;
string Http::current_url;

bool Http::use_proxy;
string Http::proxy_ip;
long Http::proxy_port;

long Http::remote_port;

string Http::get(const string &url){
    return request(url, "", false, "");
}

string Http::get(const string &url, string referer){
    return request(url, referer, false, "");
}

string Http::post(const string &url, map<string, string> params){
    string postdata = build_param_string(params);

    return request(url, "", true, postdata);
}

string Http::post(const string &url, map<string, string> params, string referer){
    string postdata = build_param_string(params);

    return request(url, referer, true, postdata);
}

string Http::get_current_url(){
    return current_url;
}

string Http::get_last_url(){
    return last_url;
}

void Http::set_proxy(string ip, long port){
    use_proxy = true;
    proxy_ip = ip;
    proxy_port = port;
}

void Http::clear_proxy(){
    use_proxy = false;
}

void Http::set_remote_port(long port){
    remote_port = port;
}

string Http::request(const string &url, string referer, bool post, const string &params){
    static string buffer = "";
    static char error_buffer[CURL_ERROR_SIZE];
    error_buffer[0] = 0;

    CURL *curl;
    CURLcode result;

    curl = curl_easy_init();

    if(curl){
        curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error_buffer);
        curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
        curl_easy_setopt(curl, CURLOPT_HEADER, 0);
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
        curl_easy_setopt(curl, CURLOPT_AUTOREFERER, 1);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, Http::writer);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &(buffer));
        curl_easy_setopt(curl, CURLOPT_COOKIEFILE, ".cookies.cj");
        curl_easy_setopt(curl, CURLOPT_COOKIEJAR, ".cookies.cj");
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST,  2);
        curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");

        if(post){
            curl_easy_setopt(curl, CURLOPT_POST, 1);
            curl_easy_setopt(curl, CURLOPT_POSTFIELDS, params.c_str());
        }

        if(referer.length()){
            curl_easy_setopt(curl, CURLOPT_REFERER, referer.c_str());
        }

        if(use_proxy){
            curl_easy_setopt(curl, CURLOPT_PROXY, proxy_ip.c_str());

            if(proxy_port){
                curl_easy_setopt(curl, CURLOPT_PROXYPORT, proxy_port);
            }
        }

        if(remote_port){
            curl_easy_setopt(curl, CURLOPT_PORT, remote_port);
        }

        result = curl_easy_perform(curl);

        curl_easy_cleanup(curl);

        if(result == CURLE_OK){
            return buffer;
        }
        else{
            return "error";
        }
    }

    return "error";
}

int Http::writer(char *data, size_t size, size_t nmemb, string *buffer){
    int result = 0;

    if(buffer != NULL){
        buffer->append(data, size * nmemb);
        result = size * nmemb;
    }

    return result;
}

string Http::urlencode(const string &str){
    string encoded = "";

    int max = str.length();

    for(int i = 0; i < max; i++){
        if(
            (48 <= str[i] && str[i] <= 57) ||
            (65 <= str[i] && str[i] <= 90) ||
            (97 <= str[i] && str[i] <= 122) ||
            (str[i] == '~' || str[i] == '!' || str[i] == '*' || str[i] == '(' || str[i] == ')' || str[i] == '\'')
        ){
            encoded.append(&str[i], 1);
        }
        else{
            encoded.append("%");
            encoded.append(dechex(str[i]));
        }
    }

    return encoded;
}

string Http::dechex(char dec){
    char dig1 = (dec&0xF0)>>4;
    char dig2 = (dec&0x0F);
    if ( 0<= dig1 && dig1<= 9) dig1+=48;    //0,48inascii
    if (10<= dig1 && dig1<=15) dig1+=97-10; //a,97inascii
    if ( 0<= dig2 && dig2<= 9) dig2+=48;
    if (10<= dig2 && dig2<=15) dig2+=97-10;

    string r;
    r.append( &dig1, 1);
    r.append( &dig2, 1);
    return r;
}

string Http::build_param_string(map<string, string> params){
    string param_string = "";

    for(map<string, string>::iterator it = params.begin(); it != params.end(); it++){
        param_string += it->first + "=" + urlencode(it->second) + "&";
    }

    param_string = param_string.substr(0, param_string.length() - 1);

    return param_string;
}

What’s happening is if I make more than a single request, the response returned by the request functions (.get(); or .post();) is just appended onto all of the previous requests’s return data.

I feel like this is something simple that I’m missing, but I can’t figure it out after Googling around for the last couple of days.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T16:38:09+00:00Added an answer on June 1, 2026 at 4:38 pm

    If you initialize the buffer on the same line:

    string Http::request(const string &url, string referer, bool post, const string &params){
        static string buffer = "";
    

    That initialization will only happen the first time you run the function, because of the static modifier. And because the write function only appends data to the buffer…

    You need to separate that on 2 lines to reset the buffer for each run:

    static string buffer;
    buffer.clear();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I am currently running into a problem where an element is coming back from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.