16,240
社区成员
发帖
与我相关
我的任务
分享
CURLcode res;
CURL* curl = curl_easy_init();
if (NULL == curl)
{
return CURLE_FAILED_INIT;
}
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
curl_easy_setopt(curl, CURLOPT_URL, "https://cas.gaoxiaobang.com/login");
curl_easy_setopt(curl, CURLOPT_HEADER, TRUE);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookie_open.txt");
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
res = curl_easy_perform(curl); //先访问登录界面,获取JSESSIONID,lt,execution
if (res != 0)
{
return 0;
}
curl_easy_cleanup(curl);
QString strHtmlGet(strResponse.c_str());
qDebug("readString = %s", qPrintable(strHtmlGet));
int index = strHtmlGet.indexOf("Set-Cookie");
strHtmlGet = strHtmlGet.right(strHtmlGet.length() - index);
QString strTemp = strHtmlGet.left(strHtmlGet.indexOf(";"));
//取得COOKIE(JSESSIONID)
QString strCookie = strTemp.right(strTemp.length() - strTemp.indexOf(":") - 2);
index = strHtmlGet.indexOf(" name=\"lt\" value=\"");
strHtmlGet = strHtmlGet.right(strHtmlGet.length() - index);
index = strHtmlGet.indexOf("LT");
strHtmlGet = strHtmlGet.right(strHtmlGet.length() - index);
//取得lt值
QString strLt = strHtmlGet.left(strHtmlGet.indexOf("\""));
index = strHtmlGet.indexOf("name=\"execution\" value=\"");
strHtmlGet = strHtmlGet.right(strHtmlGet.length() - index);
index = strHtmlGet.indexOf("value=\"") + 7;
strHtmlGet = strHtmlGet.right(strHtmlGet.length() - index);
//取得execution值
QString strExe = strHtmlGet.left(strHtmlGet.indexOf("\""));
//准备POST登录的数据
QString strData = QString(QStringLiteral("username=201521437&password=wujinyu1996<=%1&execution=%2&_eventId=submit&tenantId=712&rememberMe=false&checkFlag=false&submit=登录&hiddenView=welcome gaoxiaobang")).arg(strLt, strExe);
CURLcode res2;
CURL* curl2 = curl_easy_init();
if (curl2 == NULL)
{
return CURLE_FAILED_INIT;
}
std::string strPostRet;
std::string strPostHead;
struct curl_slist *header_opts = 0;
header_opts = curl_slist_append(header_opts, "Expect: ");
header_opts = curl_slist_append(header_opts, "Content-Type: application/x-www-form-urlencoded");
header_opts = curl_slist_append(header_opts, "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
header_opts = curl_slist_append(header_opts, "Host: cas.gaoxiaobang.com");
header_opts = curl_slist_append(header_opts, "Origin: https://cas.gaoxiaobang.com");
header_opts = curl_slist_append(header_opts, "Referer: https://cas.gaoxiaobang.com/login?service=https://wfec.class.gaoxiaobang.com/&tenantId=712");
header_opts = curl_slist_append(header_opts, "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
// curl_easy_setopt(curl2, CURLOPT_VERBOSE, 1);
// curl_easy_setopt(curl2, CURLOPT_DEBUGFUNCTION, OnDebug);
curl_easy_setopt(curl2, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(curl2, CURLOPT_SSL_VERIFYHOST, false);
curl_easy_setopt(curl2, CURLOPT_URL, "https://cas.gaoxiaobang.com/login?service=https://wfec.class.gaoxiaobang.com/&tenantId=712");
curl_easy_setopt(curl2, CURLOPT_POST, 1);
curl_easy_setopt(curl2, CURLOPT_POSTFIELDS, strData.toUtf8().data());
curl_easy_setopt(curl2, CURLOPT_COOKIEFILE, "cookie_open.txt");
curl_easy_setopt(curl2, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl2, CURLOPT_WRITEFUNCTION, OnWriteData);
curl_easy_setopt(curl2, CURLOPT_WRITEDATA, (void *)&strPostRet);
curl_easy_setopt(curl2, CURLOPT_HEADER, TRUE);
curl_easy_setopt(curl2, CURLOPT_HTTPHEADER, header_opts);
curl_easy_setopt(curl2, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl2, CURLOPT_CONNECTTIMEOUT, 10);
curl_easy_setopt(curl2, CURLOPT_TIMEOUT, 10);
res2 = curl_easy_perform(curl2);
curl_easy_cleanup(curl2);
QString strRes = strPostRet.c_str();
qDebug("readString = %s", qPrintable(strRes));
// QMessageBox message2(QMessageBox::Information, "", QString(strPostRet.c_str()), QMessageBox::Yes);
// message2.exec();
return res2;