Как проверить, успешно ли вошел в систему cURL?C ++ - PullRequest
0 голосов
/ 20 октября 2018

, поэтому я пытаюсь создать сценарий c ++, который будет входить на веб-сайт и выводить исходный код в dump.txt.Есть ли способ узнать, успешно ли вошел в систему скрипт?

код:

#include "pch.h"
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <curl/curl.h>
#include <curl/easy.h>
#include <string.h>
#include <stdlib.h>

int main(void)
{
    CURL *curl;
    CURLcode res;
    FILE* logfile;

    logfile = fopen("dump.txt" , "wb");

    curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/4.0");
        curl_easy_setopt(curl, CURLOPT_AUTOREFERER, 1);
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
        curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "");`enter code here`
        curl_easy_setopt(curl, CURLOPT_STDERR, logfile);

        // Visit the login page once to obtain a PHPSESSID cookie
        curl_easy_setopt(curl, CURLOPT_URL, "http://forum.nephrite.ro/index.php?app=core&module=global&section=login/");
        curl_easy_perform(curl);


        // Now, can actually login. First we forge the HTTP referer field, or HTS will deny the login
        curl_easy_setopt(curl, CURLOPT_REFERER, "http://forum.nephrite.ro/index.php?app=core&module=global&section=login/");
        // Next we tell LibCurl what HTTP POST data to submit
        char data[] = "ips_username=xxx&ips_password=xxx";
        char *ptrToString = data;
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
        curl_easy_perform(curl);

        curl_easy_cleanup(curl);

        fclose(logfile);
    }
    return 0;
}

1 Ответ

0 голосов
/ 22 октября 2018

используйте CURLOPT_WRITEFUNCTION & CURLOPT_WRITEDATA , чтобы захватить страницу, извлеченную скручиваемостью, и проверить, содержит ли она строку «успешно выполнен вход», что-то вроде

size_t curl_write_callback(const void * read_ptr, const size_t size,
        const size_t count, void *s_ptr)
{
    (*(string*) s_ptr).append((const char*) read_ptr, size * count);
    return count;
}

а затем

    curl_easy_setopt(curl, CURLOPT_URL, "http://forum.nephrite.ro/index.php?app=core&module=global&section=login/");
    string html;
    curl_easy_setopt(curl,CURLOPT_WRITEDATA,&html);
    curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,curl_write_callback);
    curl_easy_perform(curl);
    if(html.find_first_of("Welcome back Dave!")!=string::npos){
          // login successful!
    } else{
         // login failed
    }

просто замените Welcome back Dave! какой-либо строкой, которая представляется вам только после успешного входа в систему.и, кстати, при использовании c ++ не используйте лямбду для CURLOPT_WRITEFUNCTION, и если вы собираетесь использовать член класса для обратного вызова, функция-член класса должна быть статической, иначе вывероятно, что произойдет сбой во время выполнения .. (был там, сделал это>. <) </p>

...