curl 302重定向不工作(命令行) [英] curl 302 redirect not working (command line)

查看:1251
本文介绍了curl 302重定向不工作(命令行)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在浏览器中,导航到此网址会启动302(暂时移动)请求,该请求依次下载文件。

In the browser, navigating to this URL initiates a 302 (moved temporarily) request which in turn downloads a file.


http://www.targetsite.com/target.php/?event=download&task_id=123

当我通过Chrome网络工具查看实际发生的事情时,我发现重定向将转到动态生成的路径,在下载后立即自行取消。换句话说,即使我知道完整路径,我也没有时间手动调用它。

When I view what is actually happening via Chrome network tools I see that the redirect is going to a dynamically generated path that cancels itself immediately after download. In other words, even if I know that full path I will not have time to manually call it.

所以,如何使用命令行可以模仿浏览器动作?

So, how in using the command line can I mimic the browser actions?

我试过


curl --cookies bin / cookies.txt - O -L http://www.targetsite.com/target。 php /?event = download& task_id = 123 --compressed

curl --cookies bin/cookies.txt -O -L "http://www.targetsite.com/target.php/?event=download&task_id=123" --compressed

但这只是返回乱码。这样做的目的是以编程方式下载此文件,而无需导航到网站。不幸的是,我不能共享这里的网站,因为它是在登录后面。

but this just returns gibberish. The goal of this is to programmatically download this file without having to navigate to the site. Unfortunately I cannot share the site here as it is behind a log-in.

推荐答案

您需要提供 -L - location 选项,以便启用 curl 跟踪HTTP重定向。

You need to supply the -L or --location option in order to enable curl to follow HTTP redirects.

引用 man curl

   -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  --loca‐
          tion-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.

这篇关于curl 302重定向不工作(命令行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆