0

我正在尝试通过 HTTP 表单(使用 POST)自动登录到站点。这是我到目前为止的代码。当登录成功时,用户应该得到一个 302 重定向到另一个页面。下面的代码仅在我将 CURLOPT_NOBODY 设置为 false 时才有效此代码输出https://mytestsite.com/success.jsp,但如果我将 CURLOPT_NOBODY 设置为 1L,我只需在重定向之前获取 url ( https://mytestsite.com/status.html )。为什么是这样?

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;

}

4

0 回答 0