0

I'm really new to C++ I'm trying to do a comparison from a curl request but my problem is that my program always print the body and doesn't enter in the if statement I already searched but since I'm new to C++ I don't know what I must search exactly

#define CURL_STATICLIB
#include <iostream>
#include "curl/curl.h"
#include "httprquest.hpp"
#ifdef _DEBUG
#pragma comment (lib, "libcurl_a_debug.lib")
#else
#pragma comment (lib, "libcurl_a.lib")
#endif
#pragma comment (lib, "Normaliz.lib")
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Wldap32.lib")
#pragma comment (lib, "Crypt32.lib")
#pragma comment (lib, "advapi32.lib")


size_t write_data(void* buffer, size_t size, size_t nmemb, void* userp)
{
    return size * nmemb;
}

int auth() {
    
    std::string username = "test";
    std::string password = "test";
    std::string hwid = "11111";
    std::string data = "username=" + username + "&password=" + password + "&hwid=" + hwid;
    std::string result;
    CURL* curl;
    CURLcode res;
    curl_global_init(CURL_GLOBAL_DEFAULT);
    curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "http://test.net/something.php?");
        curl_easy_setopt(curl, CURLOPT_POST, 1);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L);
        //curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
        curl_easy_setopt(curl, CURLOPT_NOBODY, 0);
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
        
    }
    curl_global_cleanup();
    if (result.c_str() == "Access Granted#") {
        printf("granted");
        return 1;
    }
    else if (result.c_str() == "Bad Access") {
        printf("not granted");
        return 2;
    }
    else if(result.c_str() == "Bad HWIDBad Access") {
        printf("hwid error");
        return 3;
    }else {
    printf("had not work");
    return 4;
}

int main()
{
    
    auth();

}

I have tried with curl_easy_setopt(curl, CURLOPT_NOBODY, 1); which doesn't print body but i still can't do comparison I tried curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); which is the same I've tried asking to my friends but they don't know C++ so they can't guide me how can I do a comparison with website body which only will return Access Granted#, Bad Access and Bad HWIDBad Access

EDIT: I missed the else statement I added it and I saw that I always get in to the else statement i'm getting Access Granted# but my if statement won't work

Rodrigo
  • 39
  • 1
  • 8

1 Answers1

2

To help you, here is an example of a working curl program (it requests a random number page from https://www.random.org/):

#include <stdio.h>
#include <curl/curl.h>
#include <string.h>
#include <string>
#include <iostream>

using namespace std;

size_t write_data(void* ptr, size_t size, size_t nmemb, std::string* data) {
    data->append((char*)ptr, size * nmemb);
    return size * nmemb;
}

int example_curl(void) {    
    std::string response_string;

    CURL *curl;
    CURLcode res;

    const char *url = "https://www.random.org/integers/?num=1&min=1&max=100&col=5&base=10&format=html&rnd=new";

    curl = curl_easy_init();
    if (curl)
    {
        cout << "GET " << url << std::endl;

        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_string);
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
        
        cout << "Done " << std::endl;
        cout << "result " << (int)res << std::endl;
        cout <<  response_string;

    }
    else
    {
        cout << "Error" << std::endl;
    }
    
    return 0;
}

int main()
{

    example_curl();

    return 0;
}

(note that the write_data() function is a bit different from you attempt)

Further, I think you are not doing the string comparisons correctly. Change the part that checks the result to:

    if (result == "Access Granted#") {
        printf("granted");
        return 1;
    }
    else if (result == "Bad Access") {
        printf("not granted");
        return 2;
    }
    else if(result == "Bad HWIDBad Access") {
        printf("hwid error");
        return 3;
    }else {
        printf("had not work");
        return 4;
    }

Finally you can also have a look at these answers for help:

C libcurl get output into a string

Downloading a file to Ubuntu with libcurl C++, simple example doesn't work

Armand Jordaan
  • 348
  • 2
  • 11
  • Thanks a lot works like a charm i added the curl_easy_setopt(curl, CURLOPT_WRITEDATA, &result); and changed the write_data function and comparison to that one you said and all works – Rodrigo May 08 '21 at 18:47
  • @Rodrigo Note that you need to be careful to always match the arguments of the function expected for CURLOPT_WRITEFUNCTION. It does not check the function at all. I was surprised that it worked to just use a std::string* in there instead of void* but it does and that's OK because they are all pointers. But if you put an integer or a double in that fourth argument the program would have serious issues. – Zan Lynx May 08 '21 at 19:23
  • @ZanLynx Lynx ohhh i see thanks for your advice :) – Rodrigo May 08 '21 at 19:59