Transfer: https://www.cnblogs.com/X-knight/p/10589961.html
Summary:
cURL (CommandLine Uniform Resource Locator) is a file transfer tool URL syntax to work in a command line use, first published in 1997. It supports file uploads and downloads, it is a comprehensive transfer tool.
curl command a lot of parameters, just to name a part of. -v / - v parameter verbose lowercase, for printing more information, including requests to send information, which is especially useful in debugging scripts. -m / - max-time <seconds > maximum length specified process -H / - header <header> designation request header parameter -s / - slient information output reduction, such as the progress --connect-timeout <seconds> when trying to connect the specified maximum length of -x / - proxy <proxyhost [: port]> Specifies the proxy server address and port, the default port 1080 -T / - the Upload-file <file> Specifies the upload file path -o / - output <file> output file name is -d / - data / - data- ascii <data> Specifies POST content --retry <num> specifies a retry count -e / - referer <URL> Specifies the reference address - I / - head to return only the header information using a hEAD request
1, curl installed
1
|
sudo apt-get install curl
|
2, GET requests
1
2
3
4
|
curl http://www.baidu.com,回车之后,HTML内容打印在屏幕上;如果这里的URL指向的是一个文件或者一幅图都可以直接下载到本地。
curl -i "http://www.baidu.com" 显示全部信息
curl -l "http://www.baidu.com" 只显示头部信息
curl -v "http://www.baidu.com" 显示get请求全过程解析
|
wget "http://www.baidu.com" can
3, Download
1
|
curl –o linjiqin http://www.cnblogs.com/X-knight/,执行后可以看到下载进度提示,完成100%后会自动退出了,把网页保存到X-knight中。
|
It also has a capital O option is preserved in accordance with the file name on the server to a local, if you perform curl -O http://www.cnblogs.com , is being given, suggesting not find the file name, if replaced -O https://www.cnblogs.com/X-knight/p/10589004.html curl , automatically save the file as 10,589,004 .html.
4, upload
1
|
-T/--upload-file:往服务器上传文件
|
usage:
1
2
3
4
5
|
上传多个文件
curl -T "img[1-1000].png" ftp://example.com/upload/
上传多个文件
curl -T "{file1,file2}" http://www.example.com
|
5, POST method
-d or --data parameters: post request usage of:
1
|
curl -d "id=1&name=test" http://example.com/example.php ,需把请求的参数和URL分开,
|
At the same time you can use:
1
|
curl -d "id=1" -d "name=test" http://example.com/example.php ,相当于提交了两个参数。当提交的参数值中有特殊字符就需要先转义。如空格时,就需要转义成%20。
|
--data-urlencode parameters: to escape special characters automatically, without manual prior escaped.
1
|
curl --data-urlencode "name=April 1" http://example.com/example.php
|
-F or --form: upload local files to the server, usage is:
1
|
curl -F "filename=@/home/test/test.pic" http://example.com/example.php 。千万不能漏掉@符号。
|
6, set the referer
sometimes if we direct requests a URL can not be successful, it needs to determine whether referer correctly, it can be simulated by -e parameter or --referer
1
|
curl --referer http://www.example.com http://www.example.com
|
7, designated Agent the User
-A / - the User-Agent: disguised as specified Chrome browser access, usage:
1
|
curl -A "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36" www.baidu.com
|
8, forged the cookie
-b or --cookie:
There are two ways:
First, specify the parameters and values:
1
|
curl --cookie "name=xxx" http://www.example.com
|
Second, read from a file:
1
|
curl -b /cookie.txt http://www.example.com
|
9, stored cookie
-C / - JAR-cookie: cookie generated when the curl command file save operation is performed:
1
|
curl -c ./cookie.txt -d username=aaaa -d pwd=****** http://www.example.com
|
10, define the output display content
-w / - write-out: output content can be defined, such as the common code http, TCP connection time, time domain analysis, a first time the handshake time and response time, is very strong.
1, print out the http return code
1
|
curl -o /dev/null -s -w %{http_code} "http://www.baidu.com"
|
2, the response time of printing
1
|
curl -o /dev/null -s -w "time_total: %{time_total}\n" "http://www.baidu.com"
|