用libcurl填写帖子表格并获取最终网址

Fill post form with libcurl and get the final url

本文关键字:获取 表格 libcurl      更新时间:2023-10-16

我需要使用C 和libcurl在网站上授权站点具有复杂的URL,并使用HTTPS网站返回表格

Login field:
<input type="text" maxlength="64" name="edit[id]" id="edit-id" size="60" value="" class="form-text required">
Password field:
<input type="password" name="edit[pass]" id="edit-pass" value="" maxlength="20" size="30" class="form-text required">
Submit button:
<input type="submit" name="op" value="Log in" id="account_login_submit" class="form-submit submit_button button" data-role="none">

当您输入正确的登录 - password对时,站点将您重定向到另一个页面,然后使用&amp; token = ... in get parameters中的代码

我当前的代码:

CURL *curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if(curl)
{
    curl_easy_setopt(curl, CURLOPT_URL, "https://url/dialog/oauth?");
    curl_easy_setopt(curl, CURLOPT_POST, 1);
    curl_easy_setopt(curl, CURLOPT_VERBOSE, true);
    curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)");
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "edit[id]=login&edit[pass]=password");
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); 
    res = curl_easy_perform(curl);
    char url[256];
    curl_easy_getinfo(curl, CURLINFO_REDIRECT_URL, &url);
    printf("final [%s]n");
}

现在,它在curlinfo_redirect_url中没有返回,而html的html表单代码

首先,您使用的是curl_easy_getinfo(curl, CURLINFO_REDIRECT_URL..)错误,因为您应该将指针传递给char *,但是您的代码将指针传递给Char Buffer的指针。我现在在curlinfo_redirect_url man页面上添加了一个示例,以显示更好。

但最终您可能不应该使用此选项,因为它旨在显示url 它将重定向到,如果您不要求Libcurl重定向 - 您要求Libcurl关注重定向(使用curlopt_followlocation(。

如果您想找出url libcurl重定向到,请改用curlinfo_effective_url。该页面还具有一个示例(现在(以显示如何使用。