我对C++真的很陌生。我正试图从curl请求中进行比较,但我的问题是我的程序总是打印正文,并且不输入我已经搜索过的if语句,但由于我是C++的新手,我不知道我必须搜索什么
#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 Access
和Bad 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()
功能与您尝试的有点不同(
此外,我认为您没有正确地进行字符串比较。将检查结果的部分更改为:
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将输出转换为字符串
使用libcurl C++将文件下载到Ubuntu,简单的例子不;t工作