如何检查cURL是否成功登录?c ++
How to check if cURL logged in succesfully? c++
所以我正在尝试制作一个 c++ 脚本,该脚本将登录到网站并将源代码输出到转储中.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§ion=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§ion=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;
}
使用 CURLOPT_WRITEFUNCTION & CURLOPT_WRITEDATA 捕获 curl 获取的页面,并检查它是否包含"登录成功"字符串,类似于
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§ion=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++ 时,不要使用 lambda 进行CURLOPT_WRITEFUNCTION,如果要使用类成员进行回调,类成员函数必须是静态的,否则很可能会遇到运行时崩溃。(去过那里,做过那>.<(
相关文章:
- cmake在我的项目中所需的所有静态库都不成功
- 尽管测试成功,CppUnit测试核心仍被丢弃.为什么
- 如何让LLDB在成功时退出,在失败时等待
- 有没有办法知道Tracer是否成功地完全连接到了jaegerclientcpp中的jaeger后端服务器
- 密码登录程序将永远循环并显示不正确的结果
- CMake WxWidgets项目成功地在Linux上构建,但没有在Windows上构建
- 为什么 std::绑定错误参数可以成功?
- Clion显示错误,但可以使用Cmake成功构建代码
- 代码使用向量成功运行,但使用数组显示错误
- 如何检查cURL是否成功登录?c ++
- 为什么 WinInet 在通过 FQDN 连接时无法通过协商自动进行身份验证,但如果通过 IP 连接则成功?
- C++为什么我的编译器成功了,但我的计算机给出了调试错误?
- 未知的 GCC 链接器错误,但已成功构建
- 我的 SonarQube C++扫描成功,但结果仅标记重复项,而没有标记其他标记的位置
- 为什么 LoadLibrary 失败,而 LoadLibraryA 成功加载 DLL?
- 生成成功,但不会给出正确的输出
- 默认/样板代码在Visual Studio 2017中给我错误.E1574.虚幻.但构建成功了
- 即使不包含其标头,如何成功向前声明的类编译?
- 如何使用system()在c中执行2个成功的系统命令,例如连接到ftp并使用程序登录
- 如何在 win32 c++ 应用程序中成功登录后删除这些文本框