libcurl 模拟登录cas网站问题,请大神们帮忙分析下

sinat_30784251 2017-07-02 01:03:03
我的开发环境是qt5.5+vs,用了一个libcurl库,模拟登录一个cas网站,谷歌浏览器抓包截图如下






代码如下

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;


现在的问题是POST后总是返回了登录界面,请大神帮我分析下呢!
...全文
716 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
hury1001 2017-11-03
  • 打赏
  • 举报
回复
楼主解决了吗,小白也是该问题的惨绕者,如您解决了能否指导下小白,小白愿意物质酬谢!
lunqintudor 2017-10-12
  • 打赏
  • 举报
回复
请问楼主解决了吗,我也遇到了这个问题。
ArnoYu 2017-07-19
  • 打赏
  • 举报
回复
CAS login ticket 怎么获取的,这个问题怎么解决,求指教
sinat_30784251 2017-07-02
  • 打赏
  • 举报
回复
求大神。。。。。。。。。
sinat_30784251 2017-07-02
  • 打赏
  • 举报
回复
没人懂么?。。。。。。。

16,203

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧