簡體   English   中英

C++ 從 curl 進行比較

[英]C++ doing comparison from curl

我對 C++ 真的很陌生,我正在嘗試從 curl 請求進行比較,但我的問題是我的程序總是打印正文並且不輸入我已經搜索過的 if 語句,但因為我是 ZF6F897C9FDCF8EEB3C13C2F 的新手我不知道我必須准確搜索什么

#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();

}

我試過curl_easy_setopt(curl, CURLOPT_NOBODY, 1); 不打印正文但我仍然無法進行比較我試過curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); 這與我嘗試向我的朋友詢問的情況相同,但他們不知道 C++ 所以他們無法指導我如何與只會返回Access Granted#Bad AccessBad HWIDBad Access的網站正文進行比較

編輯:我錯過了我添加的 else 語句,我看到我總是進入 else 語句,我正在獲得訪問權限#但我的 if 語句不起作用

為了幫助您,這里是一個工作 curl 程序的示例(它從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;
}

(請注意write_data() function 與您嘗試的有點不同)

此外,我認為您沒有正確進行字符串比較。 將檢查結果的部分更改為:

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

最后,您還可以查看這些答案以尋求幫助:

C libcurl 得到 output 成字符串

使用 libcurl C++ 將文件下載到 Ubuntu,簡單示例不起作用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM