c++ curl模拟登陆

我们在使用一些网站时,输入用户名,密码即可登陆该网站,登陆原理就是将用户输入的用户名和密码组合成一个特定字符串,post给一个url地址,如果用户名密码正确,就可以登陆了,如果想使用程序模拟这种登陆过程,需要下面的步骤:
1 抓包工具,推荐使用HttpWatch专业版,输入用户名,密码就开始抓包,并从包的内容中分析出post的url地址和post的内容,下面是我分析的(举例说明,不是实际情况)
url地址:https://loginchina.abc.com/member/signin.htm
post内容:action=Signin&eventSubmitDoPost=any&Done=http://china.abc.com/&urlType=&formSubmit=Y&LoginId="用户名"&Password="密码"

2写程序:下面是关键的部分程序
int post_page_content(char* url)
{
   
   m_chunk.memory = (char*)malloc(1);    
   m_chunk.size = 0; 
   
   curl_easy_setopt(curl_handle, CURLOPT_URL, url);
   curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, 0);
   curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYHOST, 1);
   curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, true);
   curl_easy_setopt(curl_handle, CURLOPT_COOKIEJAR, "mycookie.txt"); 
   curl_easy_setopt(curl_handle, CURLOPT_COOKIEFILE, "mycookie.txt"); 
 
    
   curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
 
    
   curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&m_chunk);
 
    
   curl_easy_setopt(curl_handle,               CURLOPT_POSTFIELDS,"action=Signin&eventSubmitDoPost=any&Done=http://china.abc.com/&urlType=&formSubmit=Y&LoginId="用户名"&Password="密码"");
  curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
 
    
   curl_easy_perform(curl_handle); 
     return 1;
}
char *myurl = "https://loginchina.abc.com/member/signin.htm";
post_page_content(myurl);

经过这两个步,c++程序就可以模拟登陆了
C++ 中,使用 libcurl 库发送 HTTP 请求是一种常见的网络通信操作。libcurl 是一个功能强大的库,支持多种协议如 HTTP、HTTPS、FTP等。以下是一个简单的例子,展示如何使用 libcurl 发送 GET 或 POST 请求: ```cpp #include <curl/curl.h> size_t write_callback(char *data, size_t size, size_t nmemb, std::string* userp) { (*userp).append(data, size * nmemb); return size * nmemb; } void send_http_request(const char* url, const char* request_type, const char* data = nullptr) { CURL* curl = curl_easy_init(); if (curl) { std::string response; curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); // 设置回调函数接收响应 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); if (request_type == "POST") { curl_easy_setopt(curl, CURLOPT_POST, 1); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data); // 如果需要设置请求头,可以添加下面这行 // curl_easy_setopt(curl, CURLOPT_HTTPHEADER, ...); } CURLcode res = curl_easy_perform(curl); if (res != CURLE_OK) { fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); } else { printf("Response: %s\n", response.c_str()); } curl_easy_cleanup(curl); } } ``` 在这个示例里,`send_http_request` 函数接受 URL、请求类型(GET or POST)、以及可选的数据。对于 POST 请求,还需要设置 `CURLOPT_POSTFIELDS` 来传递数据。 使用这个基础框架,你可以根据实际需求调整参数和请求内容。例如,如果你想发送 JSON 数据,可以先将其编码成字符串作为 POST 数据。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值