我们在使用一些网站时,输入用户名,密码即可登陆该网站,登陆原理就是将用户输入的用户名和密码组合成一个特定字符串,post给一个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;
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)
{
}
char *myurl = "https://loginchina.abc.com/member/signin.htm";
post_page_content(myurl);
经过这两个步,c++程序就可以模拟登陆了