wget: don't follow redirects

后端 未结 5 2261
春和景丽
春和景丽 2020-12-14 05:33

How do I prevent wget from following redirects?

5条回答
  •  天涯浪人
    2020-12-14 06:21

    In general, it is not a good idea to depend on a specific number of redirects.

    For example, in order to download IntellijIdea, the URL that is promised to always resolve to the latest version of Community Edition for Linux is something like https://download.jetbrains.com/product?code=IIC&latest&distribution=linux, but if you visit that URL nowadays, you are going to be redirected twice (2 times) before you reach the actual downloadable file. In the future you might be redirected three times, or not at all.

    The way to solve this problem is with the use of the HTTP HEAD verb. Here is how I solved it in the case of IntellijIdea:

    # This is the starting URL.
    URL="https://download.jetbrains.com/product?code=IIC&latest&distribution=linux"
    echo "URL: $URL"
    
    # Issue HEAD requests until the actual target is found.
    # The result contains the target location, among some irrelevant stuff.
    LOC=$(wget --no-verbose --method=HEAD --output-file - $URL)
    echo "LOC: $LOC"
    
    # Extract the URL from the result, stripping the irrelevant stuff.
    URL=$(cut "--delimiter= " --fields=4 <<< "$LOC")
    echo "URL: $URL"
    
    # Optional: download the actual file.
    wget "$URL"
    

提交回复
热议问题