wget和curl下载JDK

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了wget和curl下载JDK相关的知识,希望对你有一定的参考价值。

有时候我们需要在服务器上安装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这句要加上,否则下载速度会变成蜗牛

执行结果如下

  1. [[email protected] ~]# 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  
  2.   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current  
  3.                                  Dload  Upload   Total   Spent    Left  Speed  
  4.   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里指定的会带上,后面重定向的就不带了,这个影响也不大,可以不加。

  1. [[email protected] 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  
  2. --2016-08-27 20:47:52--  http://download.oracle.com/otn-pub/java/jdk/8u101-b13/jdk-8u101-linux-i586.tar.gz  
  3. Resolving download.oracle.com... 206.160.170.51, 206.160.170.9  
  4. Connecting to download.oracle.com|206.160.170.51|:80... connected.  
  5. HTTP request sent, awaiting response... 302 Moved Temporarily  
  6. Location: https://edelivery.oracle.com/otn-pub/java/jdk/8u101-b13/jdk-8u101-linux-i586.tar.gz [following]  
  7. --2016-08-27 20:47:52--  https://edelivery.oracle.com/otn-pub/java/jdk/8u101-b13/jdk-8u101-linux-i586.tar.gz  
  8. Resolving edelivery.oracle.com... 60.254.134.143  
  9. Connecting to edelivery.oracle.com|60.254.134.143|:443... connected.  
  10. HTTP request sent, awaiting response... 302 Moved Temporarily  
  11. Location: https://edelivery.oracle.com/osdc-otn/otn-pub/java/jdk/8u101-b13/jdk-8u101-linux-i586.tar.gz [following]  
  12. --2016-08-27 20:47:53--  https://edelivery.oracle.com/osdc-otn/otn-pub/java/jdk/8u101-b13/jdk-8u101-linux-i586.tar.gz  
  13. Reusing existing connection to edelivery.oracle.com:443.  
  14. HTTP request sent, awaiting response... 302 Moved Temporarily  
  15. Location: http://download.oracle.com/otn-pub/java/jdk/8u101-b13/jdk-8u101-linux-i586.tar.gz?AuthParam=1472302193_c5af865490ac9f21f2605a3abee94745 [following]  
  16. --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  
  17. Connecting to download.oracle.com|206.160.170.51|:80... connected.  
  18. HTTP request sent, awaiting response... 200 OK  
  19. Length: 183456778 (175M) [application/x-gzip]  
  20. Saving to: “jdk-8u101-linux-i586.tar.gz.1”  
  21.   
  22.  2% [==>                                   ] 4,740,512    863K/s  eta 4m 40s  

 

 参考链接:http://blog.csdn.net/gsls200808/article/details/52336745

 






以上是关于wget和curl下载JDK的主要内容,如果未能解决你的问题,请参考以下文章

curl和wget下载文件

curl和wget的区别和使用

curl和wget的区别和使用

curl和wget的区别和使用

sh 使用WGET和CURL下载

Linux里面wget和curl命令区别是啥?