转自:http://blog.csdn.net/zengraoli/article/details/11522647
我们在使用一些网站时,输入用户名,密码即可登陆该网站,登陆原理就是将用户输入的用户名和密码组合成一个特定字符串,post给一个url地址,如果用户名密码正确,就可以登陆了,如果想使用程序模拟这种登陆过程,需要下面的步骤:
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++程序就可以模拟登陆了