洛谷自动打卡机C++代码实现
以下是一个使用C++实现的洛谷自动打卡示例代码,基于libcurl库进行HTTP请求。请务必注意:实际使用时需要遵守洛谷用户协议,自动化操作可能违反网站规定,请谨慎使用。
::::info[代码]
/**
* 洛谷自动打卡程序(仅供学习参考)
* 需要安装libcurl库和json库(例如vcpkg安装)
* 编译命令示例:g++ -o luogu_punch luogu_punch.cpp -lcurl -ljsoncpp
*/
#include <iostream>
#include <string>
#include <curl/curl.h>
#include <json/json.h>
using namespace std;
// 用于存储HTTP响应数据的回调函数
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) {
((string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
// 执行HTTP POST请求
string httpPost(const string& url, const string& postData) {
CURL *curl;
CURLcode res;
string readBuffer;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData.c_str());
// 设置HTTP头
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/x-www-form-urlencoded");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
// 设置响应回调
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
// 忽略SSL证书验证(生产环境不推荐)
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
res = curl_easy_perform(curl);
if(res != CURLE_OK) {
cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << endl;
}
// 清理
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}
return readBuffer;
}
// 执行HTTP GET请求
string httpGet(const string& url) {
CURL *curl;
CURLcode res;
string readBuffer;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
res = curl_easy_perform(curl);
if(res != CURLE_OK) {
cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << endl;
}
curl_easy_cleanup(curl);
}
return readBuffer;
}
int main() {
// 初始化curl全局环境
curl_global_init(CURL_GLOBAL_ALL);
string username, password;
cout << "请输入洛谷用户名: ";
cin >> username;
cout << "请输入密码: ";
cin >> password;
// 1. 登录请求(需要根据实际接口修改URL和参数)
string loginUrl = "https://www.luogu.com.cn/api/auth/userPassLogin";
string postData = "username=" + username + "&password=" + password;
string loginResponse = httpPost(loginUrl, postData);
// 解析登录响应(示例JSON解析)
Json::Value loginRes;
Json::Reader reader;
if (!reader.parse(loginResponse, loginRes)) {
cerr << "登录响应解析失败" << endl;
return 1;
}
if (loginRes["status"].asInt() != 200) {
cerr << "登录失败: " << loginRes["errorMessage"].asString() << endl;
return 1;
}
cout << "登录成功!" << endl;
// 2. 执行打卡(需要根据实际接口修改)
string punchUrl = "https://www.luogu.com.cn/index/ajax_punch";
string punchResponse = httpGet(punchUrl);
// 解析打卡结果
Json::Value punchRes;
if (!reader.parse(punchResponse, punchRes)) {
cerr << "打卡响应解析失败" << endl;
return 1;
}
if (punchRes["code"].asInt() == 200) {
cout << "打卡成功!获得" << punchRes["currentData"]["reward"].asInt() << "积分" << endl;
} else {
cerr << "打卡失败: " << punchRes["message"].asString() << endl;
}
// 清理curl全局环境
curl_global_cleanup();
return 0;
}
::::
注意事项:
- 需要先安装依赖库:
- libcurl:处理HTTP请求
- JSON库(如jsoncpp):解析JSON数据
- 实际接口参数可能需要根据洛谷最新网页结构调整
- 当前代码中的URL和参数可能已过时,需要自行抓包获取最新接口
- 频繁自动操作可能触发反爬机制
- 密码在控制台输入会明文显示,建议使用配置文件或环境变量存储敏感信息
请务必:
- 遵守洛谷用户协议和相关法律法规
- 不要用于恶意或高频访问
- 尊重网站服务器负载
建议先通过浏览器开发者工具分析实际网络请求,替换代码中的接口地址和参数。