我正在尝试通过HTTP表单(使用POST)自动登录到站点。这是我到目前为止的代码。登录成功后,用户应该获得302重定向到另一个页面。仅当我将CURLOPT_NOBODY设置为false时,下面的代码才能工作。此代码输出https://mytestsite.com/success.jsp,但如果我将CURLOPT_NOBODY设置为1L,我只需在重定向(https://mytestsite.com/status.html)之前获取url。这是为什么?
int login()
{
CURL *curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, "https://mytestsite.com/login.do");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/4.0");
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "/home/cookies.txt");
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "/home/cookies.txt");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "loginId=person1@mytestsite.com&password=mypassword");
// Disable output from curl_easy_perform
curl_easy_setopt(curl, CURLOPT_NOBODY, 0L);
res = curl_easy_perform(curl);
char *url;
curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
fprintf(stdout, "#####\n");
fprintf(stdout, "%s\n", url);
fprintf(stdout, "#####\n");
curl_easy_cleanup(curl);
return 0;
}