Curl 无法遵循“此页面应自动重定向”

Curl 无法遵循“此页面应自动重定向”

我正在尝试自动登录本学期要查看的课程网页。我知道我发出的 POST 请求已正确完成,因为我进入了只有成功登录后才能进入的中间重定向页面。

我基本上访问了其中一个This page should automatically redirect. If nothing is happening please use the continue link below.页面。不幸的是,由于 curl 没有遵循这个最终重定向,它似乎没有设置最后的 cookie 来保留我的会话。

我一直在阅读man有关 curl 的内容,但似乎找不到正确的方法来完成此操作(我已经尝试过--max-time--max-redirs没有效果)。

谁能告诉我该怎么做才能解决这个问题?

以下是我正在研究的内容

LOGINURL="http://www.[redacted].edu/login/index.php" # This is not https because they don't support it.
COURSEURL="http://www.[redacted].edu/course/[redacted]"
USERAGENT="Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0"
COOKIEJAR="${HOME}/edu.cookies"
POSTDATA="[redacted]"
curl -o "index.html" --referer "${LOGINURL}" --user-agent "${USERAGENT}" --cookie-jar "${COOKIEJAR}" --data "${POSTDATA}" "${LOGINURL}"
curl -o "course.html" --referer "${LOGINURL}" --user-agent "${USERAGENT}" --cookie "${COOKIEJAR}" "${COURSEURL}"

PS,中间重定向页面上提供的链接与 COURSEURL url 是相同的链接,因此手动卷曲它不起作用。

答案1

我觉得自己太愚蠢了。这个-L选项就是我所需要的。从手册页中:

-L, --location
              (HTTP/HTTPS)  If  the  server reports that the requested page has moved to a different location (indicated with a Location: header and a 3XX response code),
              this option will make curl redo the request on the new place. If used together with -i, --include or -I, --head, headers from all requested  pages  will  be
              shown.  When  authentication is used, curl only sends its credentials to the initial host. If a redirect takes curl to a different host, it won't be able to
              intercept the user+password. See also --location-trusted on how to change this. You can limit the amount of redirects to follow by  using  the  --max-redirs
              option.

              When  curl follows a redirect and the request is not a plain GET (for example POST or PUT), it will do the following request with a GET if the HTTP response
              was 301, 302, or 303. If the response code was any other 3xx code, curl will re-send the following request using the same unmodified method.

相关内容