@@ -134,16 +134,30 @@ curl URL --silent
134134使用选项` -O ` 将下载的数据写入到文件,必须使用文件的绝对地址:
135135
136136``` shell
137- curl http://wangchujiang .com/text.iso --silent -O
137+ curl http://example .com/text.iso --silent -O
138138```
139139
140140选项` -o ` 将下载数据写入到指定名称的文件中,并使用` --progress ` 显示进度条:
141141
142142``` shell
143- curl http://wangchujiang .com/test.iso -o filename.iso --progress
143+ curl http://example .com/test.iso -o filename.iso --progress
144144# ######################################## 100.0%
145145```
146146
147+ ** 不输出错误和进度信息**
148+
149+ ` -s ` 参数将不输出错误和进度信息。
150+
151+ ``` shell
152+ curl -s https://www.example.com
153+ # 上面命令一旦发生错误,不会显示错误信息。不发生错误的话,会正常显示运行结果。
154+ ```
155+
156+ 如果想让 curl 不产生任何输出,可以使用下面的命令。
157+
158+ ``` shell
159+ curl -s -o /dev/null https://google.com
160+ ```
147161** 断点续传**
148162
149163curl能够从特定的文件偏移处继续下载,它可以通过指定一个便宜量来下载部分文件:
@@ -165,20 +179,6 @@ curl -C -URL
165179curl --referer http://www.google.com http://wangchujiang.com
166180```
167181
168- ** 用curl设置cookies**
169-
170- 使用` --cookie "COKKIES" ` 选项来指定cookie,多个cookie使用分号分隔:
171-
172- ``` shell
173- curl http://wangchujiang.com --cookie " user=root;pass=123456"
174- ```
175-
176- 将cookie另存为一个文件,使用` --cookie-jar ` 选项:
177-
178- ``` shell
179- curl URL --cookie-jar cookie_file
180- ```
181-
182182** 用curl设置用户代理字符串**
183183
184184有些网站访问会提示只能使用IE浏览器来访问,这是因为这些网站设置了检查用户代理,可以使用curl把用户代理设置为IE,这样就可以访问了。使用` --user-agent ` 或者` -A ` 选项:
@@ -212,6 +212,11 @@ curl URL --max-filesize bytes
212212
213213如果文件大小超出限制,命令则返回一个非0退出码,如果命令正常则返回0。
214214
215+ ``` shell
216+ curl --limit-rate 200k https://example.com
217+ # 上面命令将带宽限制在每秒 200K 字节。
218+ ```
219+
215220** 用curl进行认证**
216221
217222使用curl选项 -u 可以完成HTTP或者FTP的认证,可以指定密码,也可以不指定密码在后续操作中输入密码:
@@ -248,7 +253,26 @@ curl -v "http://www.wangchujiang.com" # 显示get请求全过程解析
248253** post请求**
249254
250255``` shell
251- curl -d " param1=value1¶m2=value2" " http://www.wangchujiang.com"
256+ $ curl -d " param1=value1¶m2=value2" " http://www.wangchujiang.com/login"
257+
258+ curl -d' login=emma&password=123' -X POST https://wangchujiang.com/login
259+ # 或者
260+ $ curl -d ' login=emma' -d ' password=123' -X POST https://wangchujiang.com/login
261+ ```
262+
263+
264+ ` --data-urlencode ` 参数等同于 ` -d ` ,发送 ` POST ` 请求的数据体,区别在于会自动将发送的数据进行 ` URL ` 编码。
265+
266+ ``` shell
267+ curl --data-urlencode ' comment=hello world' https://wangchujiang.com/login
268+ # 上面代码中,发送的数据hello world之间有一个空格,需要进行 URL 编码。
269+ ```
270+
271+ ** 读取本地文本文件的数据,向服务器发送**
272+
273+ ``` shell
274+ curl -d ' @data.txt' https://wangchujiang.com/upload
275+ # 读取data.txt文件的内容,作为数据体向服务器发送。
252276```
253277
254278** json格式的post请求**
@@ -257,6 +281,129 @@ curl -d "param1=value1¶m2=value2" "http://www.wangchujiang.com"
257281curl -l -H " Content-type: application/json" -X POST -d ' {"phone":"13521389587","password":"test"}' http://wangchujiang.com/apis/users.json
258282```
259283
284+ ** 向服务器发送 Cookie**
285+
286+ 使用` --cookie "COKKIES" ` 选项来指定cookie,多个cookie使用分号分隔:
287+
288+ ``` shell
289+ curl http://wangchujiang.com --cookie " user=root;pass=123456"
290+ ```
291+
292+ 将cookie另存为一个文件,使用` --cookie-jar ` 选项:
293+
294+ ``` shell
295+ curl URL --cookie-jar cookie_file
296+ ```
297+
298+
299+ ` -b ` 参数用来向服务器发送 Cookie。
300+
301+ ``` shell
302+ curl -b ' foo=bar' https://taobao.com
303+ # 上面命令会生成一个标头Cookie: foo=bar,向服务器发送一个名为foo、值为bar的 Cookie。
304+ ```
305+
306+ ``` shell
307+ curl -b ' foo1=bar' -b ' foo2=baz' https://taobao.com
308+ # 上面命令发送两个 Cookie。
309+
310+ ` ` ` shell
311+ curl -b cookies.txt https://www.taobao.com
312+ # 上面命令读取本地文件 cookies.txt,里面是服务器设置的 Cookie(参见-c参数),将其发送到服务器。
313+ ` ` `
314+
315+ ** Cookie 写入一个文件**
316+
317+ ` ` ` shell
318+ curl -c cookies.txt https://www.taobao.com
319+ # 上面命令将服务器的 HTTP 回应所设置 Cookie 写入文本文件cookies.txt。
320+ ` ` `
321+
322+ ** 请求的来源**
323+
324+ ` -e` 参数用来设置 ` HTTP` 的标头 ` Referer` ,表示请求的来源。
325+
326+ ` ` ` shell
327+ curl -e ' https://taobao.com?q=example' https://www.example.com
328+ # 上面命令将Referer标头设为 https://taobao.com?q=example。
329+ ` ` `
330+
331+ ` -H` 参数可以通过直接添加标头 ` Referer` ,达到同样效果。
332+
333+ ` ` ` shell
334+ curl -H ' Referer: https://taobao.com?q=example' https://www.example.com
335+ ` ` `
336+
337+ ** 上传二进制文件**
338+
339+ ` -F` 参数用来向服务器上传二进制文件。
340+
341+ ` ` ` shell
342+ curl -F ' file=@photo.png' https://taobao.com/profile
343+ # 上面命令会给 HTTP 请求加上标头 Content-Type: multipart/form-data ,然后将文件photo.png作为file字段上传。
344+ ` ` `
345+
346+ ` -F` 参数可以指定 ` MIME` 类型。
347+
348+ ` ` ` shell
349+ curl -F ' file=@photo.png;type=image/png' https://taobao.com/profile
350+ # 上面命令指定 MIME 类型为image/png,否则 curl 会把 MIME 类型设为 application/octet-stream。
351+ ` ` `
352+
353+ ` -F` 参数也可以指定文件名。
354+
355+ ` ` ` shell
356+ curl -F ' file=@photo.png;filename=me.png' https://taobao.com/profile
357+ # 上面命令中,原始文件名为photo.png,但是服务器接收到的文件名为me.png。
358+ ` ` `
359+
360+ ** 设置请求头**
361+
362+ ` -H` 参数添加 ` HTTP` 请求的标头。
363+
364+ ` ` ` shell
365+ curl -H ' Accept-Language: en-US' https://google.com
366+ # 上面命令添加 HTTP 标头 Accept-Language: en-US。
367+ ` ` `
368+
369+ ` ` ` shell
370+ curl -H ' Accept-Language: en-US' -H ' Secret-Message: xyzzy' https://google.com
371+ # 上面命令添加两个 HTTP 标头。
372+ ` ` `
373+
374+ ` ` ` shell
375+ curl -d ' {"login": "emma", "pass": "123"}' -H ' Content-Type: application/json' https://google.com/login
376+ # 上面命令添加 HTTP 请求的标头是 Content-Type: application/json,然后用 -d 参数发送 JSON 数据。
377+ ` ` `
378+
379+ ** 跳过 SSL 检测**
380+
381+ ` ` ` shell
382+ curl -k https://www.example.com
383+ # 上面命令不会检查服务器的 SSL 证书是否正确。
384+ ` ` `
385+
386+ ** 请求跟随服务器的重定向**
387+
388+ ` -L` 参数会让 ` HTTP` 请求跟随服务器的重定向。` curl` 默认不跟随重定向。
389+
390+ ` ` ` shell
391+ curl -L -d ' tweet=hi' https://api.example.com/tweet
392+ ` ` `
393+
394+ ** 调试参数**
395+
396+ ` -v` 参数输出通信的整个过程,用于调试。
397+
398+ ` ` ` shell
399+ curl -v https://www.example.com
400+ # --trace参数也可以用于调试,还会输出原始的二进制数据。
401+ ` ` `
402+
403+ ` ` ` shell
404+ $ curl --trace - https://www.example.com
405+ ` ` `
406+
260407** 获取本机外网ip**
261408
262409` ` ` shell
0 commit comments