歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> 如何啟用curl命令HTTP2支持

如何啟用curl命令HTTP2支持

日期:2017/2/28 13:54:59   编辑:Linux教程

當我們直接使用 curl 去請求一個 https 頁面時,默認可以看到其默認返回的是 HTTP1.1 的 response。現在使用 HTTP2 的網站越來越多,技術也越來越成熟,如何啟用 curl 命令 HTTP 2 支持就成為了一個問題。

curl -I https://nghttp2.org/

當我們試圖用 http2 參數時,會返回一個未支持協議的「curl: (1) Unsupported protocol」錯誤:

curl --http2 -I https://nghttp2.org/

使用如下命令我們可以看到 curl 版本:

curl --version

從上圖中,我們可以看到當前 curl 的版本及支持的協議以及功能特性沒有支持 HTTP2。

啟用curl命令HTTP2支持

編譯安裝nghttp2

為了讓 curl 支持 HTTP2 我們需要安裝 nghttp2(http2 的 C 語言庫):

#安裝編譯工具等
sudo apt-get install git g++ make binutils autoconf automake autotools-dev libtool pkg-config \
  zlib1g-dev libcunit1-dev libssl-dev libxml2-dev libev-dev libevent-dev libjansson-dev \
  libjemalloc-dev cython python3-dev python-setuptools

#編譯安裝nghttp2
git clone https://github.com/tatsuhiro-t/nghttp2.git
cd nghttp2
autoreconf -i
automake
autoconf
./configure
make
sudo make install

升級curl版本

cd ~
sudo apt-get build-dep curl

wget http://curl.haxx.se/download/curl-7.46.0.tar.bz2
tar -xvjf curl-7.46.0.tar.bz2
cd curl-7.46.0
./configure --with-nghttp2=/usr/local --with-ssl
sudo make && make install

echo '/usr/local/lib' > /etc/ld.so.conf.d/local.conf
ldconfig

升級完版本之後,我們再查看 curl 版本時會發布特性中會增加 HTTP2 功能支持。此時 –http2 參數就可以正常使用了:

curl --http2 -I https://nghttp2.org

測試curl with http2

我們再使用如下命令測試 winclient 主頁看看:

curl --http2 -I https://www.winclient.cn

返回結果如下:

curl用法:獲取網站的header頭及狀態碼 http://www.linuxidc.com/Linux/2015-11/125325.htm

Linux工具之curl與wget高級使用 http://www.linuxidc.com/Linux/2015-05/117381.htm

Ubuntu 用戶安裝下載器 cURL 7.36.0 http://www.linuxidc.com/Linux/2014-05/102269.htm

Linux curl使用簡單介紹 http://www.linuxidc.com/Linux/2008-01/10891.htm

Unix下Curl的使用方法及常用功能記錄分享 http://www.linuxidc.com/Linux/2012-08/69154.htm

curl命令使用 http://www.linuxidc.com/Linux/2014-09/107018.htm

Copyright © Linux教程網 All Rights Reserved