《CURL使用全解指南》是一本全面解析CURL命令行工具的手册,详细介绍了其在网络数据传输中的应用技巧与实战案例。适合网络开发者阅读学习。
1. 访问HTTP页面内容,并输出到标准输出:`curl http://www.neocanable.com`
2. 生成文件并以远程文件名保存:`curl -o index.html http://www.neocanable.com` 或者使用 `wget http://www.neocanable.com` 或者 `curl http://www.neocanable.com > index.html`
3. 添加代理访问网页:`curl -x xxx.xxx.xxx.xxx http://www.neocanable.com`
4. 设置浏览器信息:
```
curl -A 浏览器信息 http://www.neocanable.com
```
5. 批量下载文件并重新命名和分类正则使用,如下载后的文件是`demo1-001.html`:
```shell
curl http://www.xxx.com/action/[1-100].html > /dev/null
curl -o #1_#2 http://www.xxx.com/~{demo1,demo2}/[1-100].html
```
创建需要的目录:
```shell
curl -o --create-dirs http://www.xxx.com/~{demo1,demo2}/[1-100].html
```
6. 分块下载文件:先下`aa.zip`的前 1M,然后再下剩下的:
```shell
curl -r 0-1024 http://www.xxx.com/aa.zip
curl -r 1025- http://www.xxx.com/aa.zip
```
7. FTP访问:
```shell
curl -u username:password ftp://www.xxx.com
```
8. 添加端口进行FTP连接:
```shell
curl -u username:password -P8899 ftp://www.xxx.com
```
上传文件到ftp服务器:
```shell
curl -T /home/neo/demo.jpg -u username:password ftp://www.xxx.com
```
9. 测试参数:
测试站点响应时间:
```shell
curl -o /dev/null -s -w %{time_connect}:%{time_starttransfer}:%{time_total} www.google.com
```
查看HTTP状态码:
```shell
curl -o /dev/null -s -w %{http_code} http://www.neocanable.com
```
网页或文件大小:
```shell
curl -o /dev/null -s -w %{size_header} http://www.neocanable.com
```
10. POST和GET请求:
GET 请求:`curl param1=name¶ms2=pass http://www.xxx.com`
POST 请求: `curl -d param1=name¶ms2=pass http://www.xxx.com`
11. 响应超时:
```shell
curl -m 40 http://www.xxx.com
```
或者使用:`curl --timeout 40 http://www.xxx.com`
12. 网站头部信息: `curl -I http://www.neocanable.com`
13. 跟踪URL跳转:
```shell
curl -L http://url.cn/2yQFfd
```
14. 正确的给 URL 编码:
```shell
curl --data-urlencode http://www.xxx.com/action?name=张三&sex=男
```
15. 限制URL传输速度:
```shell
curl --limit-rate http://www.xxx.com/action
```
16. 限制下载文件大小:
```shell
curl --max-filesize 1024 http://www.xxx.com/action
```
超过设定的大小将不执行操作,并且返回错误。
17. Curl 错误代码:
- `1`: 不支持的协议。
- `2`: 初始化失败。
- `3`: URL格式错误。语法不正确。
- `5`: 无法解析代理服务器名或IP地址
- `6`: 无法解析主机名或者 IP 地址
- `7`: 无法连接到远程主机
- `8`: FTP 非正常应答。
- `9`: FTP 访问被拒绝。
- `11`: FTP 非正常的 PASS 应答,cURL 无法解析发送给 PASS 请求的响应数据。
- `13`: cURL 无法处理 PASV 命令
- `14`: cURL 不能正确解析服务器返回的227应答行。
- `15`: FTP 连接错误,如 IP 地址或端口号无效等。
- `17`: FTP 操作失败,在发送 STOR 或 APPE 命令时出现错误。
- `18`: 部分文件