在visual c ++上使用libcurl登录网站,如何检查登录成功?

时间:2015-09-25 06:38:37

标签: c++ libcurl

我使用该演示登录网站“http://www.hackthissite.org/user/login/”。 并且来自控制台的返回值是许多html信息。就像:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title>Hack This Site! :: Login</title>
<meta name="verify-v1" content="s/YXn7eQrMBoF9PL5jLJDiWpAxEXpJzE9JLg/zM4C2Y=" />
Freenode for general discussion or any questions you h...</div>
............
<br />
<br />
<span style="font-size: 10px;"><a href="/news/view/690">read more...</a> | <a 
</body> 
</html> 

但我不知道是否代表登录成功? 我的演示代码是:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>


int main()
{

curl_global_init( CURL_GLOBAL_ALL );
CURL * myHandle = curl_easy_init ( );

// Set up a couple initial paramaters that we will not need to mofiy later.
curl_easy_setopt(myHandle, CURLOPT_USERAGENT, "Mozilla/4.0");
curl_easy_setopt(myHandle, CURLOPT_AUTOREFERER, 1 );
curl_easy_setopt(myHandle, CURLOPT_FOLLOWLOCATION, 1 );
curl_easy_setopt(myHandle, CURLOPT_COOKIEFILE, "");

// Visit the login page once to obtain a PHPSESSID cookie
curl_easy_setopt(myHandle, CURLOPT_URL, "http://www.hackthissite.org/user/login/");
curl_easy_perform( myHandle );


// Now, can actually login. First we forge the HTTP referer field, or HTS will deny the login
curl_easy_setopt(myHandle, CURLOPT_REFERER, "http://www.hackthissite.org/user/login/");
// Next we tell LibCurl what HTTP POST data to submit
char *data="username=your_username_here&password=your_password_here";
curl_easy_setopt(myHandle, CURLOPT_POSTFIELDS, data);
curl_easy_perform( myHandle );

curl_easy_cleanup( myHandle );


return 0;
}

请帮助我,非常感谢你!

2 个答案:

答案 0 :(得分:0)

据我所知,有两种方法。 从上一个 curl_easy_perform 函数中获取返回值会更容易一些:

 CURLcode res;
 res = curl_easy_perform(myHandle);

然后你必须决定如何处理结果 - 是否与CURLE_OK或你需要的结果进行比较。

更长一点的方法是在清理之前使用 curl_easy_getinfo 函数和 myHandle 变量。在那里你可以找到由函数填充的结构中的CURLINFO_RESPONSE_CODE。

答案 1 :(得分:0)

您应该只使用CURLOPT_WRITEDATACURLOPT_WRITEFUNCTION来获取源代码。然后,juste分析它以了解这是否是成功连接的源代码。 以下是恢复源代码的示例:getinmemory.c

相关问题