我想我已经在stackoverflow上阅读了每个libcurl帖子相关的问题,但我仍然无法让我的登录代码工作。我复制了其他示例帖子程序中的代码以及来自https://www.hackthissite.org/articles/read/1078的代码 它仍然无法正常工作。代码本身没有错误,但最终的get会保持重定向到https://www.masteringbiology.com/site/notloggedin.html。 我正在尝试登录并使用cookie访问网站上的特定页面。 我100%自学成才,非常感谢帮助。
#include <stdio.h>
#include <iostream>
#include <curl\curl.h>
#include<fstream>
#include <cstdio>
#include <stdlib.h>
using namespace std;
int main(void)
{
CURLcode res;
// string URL= "https://session.masteringbiology.com/myct/assignmentPrintViewassignmentID=6607103";
CURL* curly= curl_easy_init();
curl_global_init(CURL_GLOBAL_ALL);
if(curly){
cout<<"go!!!"<<endl;
curl_easy_setopt(curly, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curly, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_easy_setopt(curly, CURLOPT_USERAGENT, "Mozilla/4.0");
curl_easy_setopt(curly, CURLOPT_AUTOREFERER, 1 );
curl_easy_setopt(curly, CURLOPT_FOLLOWLOCATION, 1 );
curl_easy_setopt(curly, CURLOPT_COOKIEFILE, "");
curl_easy_setopt(curly, CURLOPT_URL,
"https://www.masteringbiology.com/site/login.html");
curl_easy_perform( curly );
// Now, can actually login. First we forge the HTTP referer field
curl_easy_setopt(curly, CURLOPT_REFERER,
"https://www.masteringbiology.com/site/login.html");
// Next we tell LibCurl what HTTP POST data to submit
char *data="nme=xxx&pwd=yyy";
curl_easy_setopt(curly, CURLOPT_POSTFIELDS, data);
curl_easy_perform( curly );
//todoc
curl_easy_setopt(curly, CURLOPT_HTTPGET, 1L);
//curl_easy_setopt(curly, CURLOPT_URL, URL.c_str());
curl_easy_setopt(curly, CURLOPT_URL,
"https://session.masteringbiology.com/myct/mastering#/");
FILE * filename1;
filename1=fopen("filetest.txt","w+");
if(filename1) {
// write the page body to this file handle
curl_easy_setopt(curly, CURLOPT_WRITEDATA, filename1);
// get it!
res=curl_easy_perform(curly);
// close the header file
fclose(filename1);
}
if (res==CURLE_OK){
cout<<"Yeah!!!";
}
else{cout<<"oh no!!! "<<res;}
}
else{
fprintf(stderr, "curl initialization failure");
return 0;
}
curl_easy_cleanup(curly);
return 0;
}
以下是登录表单的html
<!-- Mastering hidden form -->
<form name="hiddenForm" id="hiddenForm" class="hidden"
method="POST" action="https://session.masteringbiology.com/login"
target="_top">
<input type="hidden" id="authProvider" name="authProvider"
value="SMS" />
<input type="hidden" id="username" name="username" value=""
/>
<input type="hidden" id="password" name="password" value=""
/>
<input type="hidden" id="passwordEncType" name="passwordEncType" value="" />
</form>
<!-- end Mastering hidden form -->
<form class="form-stacked has-validation" name="loginForm"
id="loginForm" method="post" action="#" target="_top" autocomplete="off"
data-errorMsg="Please supply the information for all required fields marked
below.">
<label for="nme">Username</label>
<div class="group nowrap">
<input type="text" name="nme" id="nme" class="medium-
width required-field">
</div>
<label for="pwd">Password</label>
<div class="group nowrap">
<input type="password" name="pwd" id="pwd"
class="medium-width password required-field">
</div>
<button type="submit" class="button button-big-icon bg-
color-match uppercase mar-top-x1Half">
<span aria-hidden="true" data-icon="" class="left">
</span>Sign In</button>