有時候我們需要在Linux服務器上安裝Oracle JDK,這時候我們發現直接復制網頁上面的地址是不能下載的
以下載jdk-8u101-linux-i586.tar.gz為例,右鍵查看到的地址為http://download.oracle.com/otn-pub/java/jdk/8u101-b13/jdk-8u101-linux-i586.tar.gz,但是這個地址並不能直接下載
通過浏覽器開發工具觀察我們發現,下載時經過了3次重定向才拿到最終的下載地址,並且Cookies中有個參數必須帶上
將這幾個302跳轉復制成curl觀察一下
第一次重定向
curl "http://download.oracle.com/otn-pub/java/jdk/8u101-b13/jdk-8u101-linux-i586.tar.gz" -H "Host: download.oracle.com" -H "User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" -H "Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3" --compressed -H "Referer: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html" -H "Cookie: s_nr=1472257078143; ELOQUA=GUID=e0964a7736384dc98085509e60e04539; s_cc=true; gpw_e24=http"%"3A"%"2F"%"2Fwww.oracle.com"%"2Ftechnetwork"%"2Fjava"%"2Fjavase"%"2Fdownloads"%"2Fjdk8-downloads-2133151.html; s_sq="%"5B"%"5BB"%"5D"%"5D; oraclelicense=accept-securebackup-cookie" -H "Connection: keep-alive"第二次重定向
curl "https://edelivery.oracle.com/otn-pub/java/jdk/8u101-b13/jdk-8u101-linux-i586.tar.gz" -H "Host: edelivery.oracle.com" -H "User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" -H "Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3" --compressed -H "Referer: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html" -H "Cookie: ARU_LANG=US; s_nr=1472257078143; ELOQUA=GUID=e0964a7736384dc98085509e60e04539; s_cc=true; gpw_e24=http"%"3A"%"2F"%"2Fwww.oracle.com"%"2Ftechnetwork"%"2Fjava"%"2Fjavase"%"2Fdownloads"%"2Fjdk8-downloads-2133151.html; s_sq="%"5B"%"5BB"%"5D"%"5D; oraclelicense=accept-securebackup-cookie" -H "Connection: keep-alive"第三次重定向
curl "https://edelivery.oracle.com/osdc-otn/otn-pub/java/jdk/8u101-b13/jdk-8u101-linux-i586.tar.gz" -H "Host: edelivery.oracle.com" -H "User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" -H "Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3" --compressed -H "Referer: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html" -H "Cookie: ARU_LANG=US; s_nr=1472257078143; ELOQUA=GUID=e0964a7736384dc98085509e60e04539; s_cc=true; gpw_e24=http"%"3A"%"2F"%"2Fwww.oracle.com"%"2Ftechnetwork"%"2Fjava"%"2Fjavase"%"2Fdownloads"%"2Fjdk8-downloads-2133151.html; s_sq="%"5B"%"5BB"%"5D"%"5D; oraclelicense=accept-securebackup-cookie" -H "Connection: keep-alive"真實下載
curl "http://download.oracle.com/otn-pub/java/jdk/8u101-b13/jdk-8u101-linux-i586.tar.gz?AuthParam=1472300350_2a4acf9c53c18478db4839cbf7ec69c6" -H "Host: download.oracle.com" -H "User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" -H "Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3" --compressed -H "Referer: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html" -H "Cookie: s_nr=1472257078143; ELOQUA=GUID=e0964a7736384dc98085509e60e04539; s_cc=true; gpw_e24=http"%"3A"%"2F"%"2Fwww.oracle.com"%"2Ftechnetwork"%"2Fjava"%"2Fjavase"%"2Fdownloads"%"2Fjdk8-downloads-2133151.html; s_sq="%"5B"%"5BB"%"5D"%"5D; oraclelicense=accept-securebackup-cookie" -H "Connection: keep-alive"其中真實下載地址中的AuthParam每次都會變化。
curl要通過-O參數才能下載文件,否則只是將內容輸出到控制台。
所以curl方式的下載命令為
curl -L "http://download.oracle.com/otn-pub/java/jdk/8u101-b13/jdk-8u101-linux-i586.tar.gz" -H "Cookie: oraclelicense=accept-securebackup-cookie" -H "Connection: keep-alive" -O
其中-L參數能夠追蹤重定向的地址,不加-L只能返回302的結果,cookies中有用的內容是同意協議那一部分,Connection: keep-alive這句要加上,否則下載速度會變成蝸牛
執行結果如下
[root@myCentOS ~]# curl -L "http://download.oracle.com/otn-pub/java/jdk/8u101-b13/jdk-8u101-linux-i586.tar.gz" -H "Cookie: oraclelicense=accept-securebackup-cookie" -H "Connection: keep-alive" -O
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
8 174M 8 14.1M 0 0 404k 0 0:07:23 0:00:35 0:06:48 463k
同樣的wget方式的下載命令為,wget的好處可以自動重定向,不用我們操心
wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u101-b13/jdk-8u101-linux-i586.tar.gz
其中 --no-check-certificate表示不校驗SSL證書,因為中間的兩個302會訪問https,會涉及到證書的問題,不校驗能快一點,影響不大,另一個--no-cookies表示不使用cookies,當然首次在header裡指定的會帶上,後面重定向的就不帶了,這個影響也不大,可以不加。
[root@mycentos downloads]# wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u101-b13/jdk-8u101-linux-i586.tar.gz
--2016-08-27 20:47:52-- http://download.oracle.com/otn-pub/java/jdk/8u101-b13/jdk-8u101-linux-i586.tar.gz
Resolving download.oracle.com... 206.160.170.51, 206.160.170.9
Connecting to download.oracle.com|206.160.170.51|:80... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: https://edelivery.oracle.com/otn-pub/java/jdk/8u101-b13/jdk-8u101-linux-i586.tar.gz [following]
--2016-08-27 20:47:52-- https://edelivery.oracle.com/otn-pub/java/jdk/8u101-b13/jdk-8u101-linux-i586.tar.gz
Resolving edelivery.oracle.com... 60.254.134.143
Connecting to edelivery.oracle.com|60.254.134.143|:443... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: https://edelivery.oracle.com/osdc-otn/otn-pub/java/jdk/8u101-b13/jdk-8u101-linux-i586.tar.gz [following]
--2016-08-27 20:47:53-- https://edelivery.oracle.com/osdc-otn/otn-pub/java/jdk/8u101-b13/jdk-8u101-linux-i586.tar.gz
Reusing existing connection to edelivery.oracle.com:443.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: http://download.oracle.com/otn-pub/java/jdk/8u101-b13/jdk-8u101-linux-i586.tar.gz?AuthParam=1472302193_c5af865490ac9f21f2605a3abee94745 [following]
--2016-08-27 20:47:53-- http://download.oracle.com/otn-pub/java/jdk/8u101-b13/jdk-8u101-linux-i586.tar.gz?AuthParam=1472302193_c5af865490ac9f21f2605a3abee94745
Connecting to download.oracle.com|206.160.170.51|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 183456778 (175M) [application/x-gzip]
Saving to: “jdk-8u101-linux-i586.tar.gz.1”
2% [==> ] 4,740,512 863K/s eta 4m 40s
Linux wget命令詳解 http://www.linuxidc.com/Linux/2012-08/67837.htm
Linux 下使用 wget/aria2 進行離線迅雷批量下載 http://www.linuxidc.com/Linux/2011-10/46052.htm
Linux使用wget請求地址時報錯 http://www.linuxidc.com/Linux/2011-07/39345.htm
Linux下載命令wget使用詳解 http://www.linuxidc.com/Linux/2011-01/30980.htm
wget 使用大全 http://www.linuxidc.com/Linux/2008-09/15722.htm
Linux 命令行下載工具 wget 的使用技巧 http://www.linuxidc.com/Linux/2007-10/8293.htm
wget 命令實例 http://www.linuxidc.com/Linux/2014-10/108733.htm