curl 命令详解(转)
命令事例
1、读取网页,将网页内容全部输出
curl http://www.linuxidc.com
2、保存网页、下载文件
以page.html命名下载网页:curl –o page.html http://www.linuxidc.com
以服务器上的文件名命名网页:curl –O http://cgi2.tky.3web.ne.jp/~zzh/screen1.JPG
批量下载文件:curl -O http://cgi2.tky.3web.ne.jp/~zzh/screen[1-10].JPG
3、使用代理服务器及其端口
curl –x 127.0.0.1:8580 –o page.html http://www.linuxidc.com
4、使用cookie来记录session信息
cookie 信息存到cookie1.txt中:curl –o page.html –D cookie1.txt http://www.linuxidc.com
使用上次的cookie并生成新的cookie:curl –o page.html –D cookie2.txt -b cookie2.txt http://www.linuxidc.com
5、断点续传
比如下载screen1.JPG中,突然掉线了,可以这样开始续传:curl -c -O http://cgi2.tky.3wb.ne.jp/~zzh/screen1.JPG
另外可以用-r选项进行分块下载
http://blog.csdn.net/cheng_fangang/article/details/8662250
- 发送POST请求:
- 如果传输文件:curl -F "blob=@tmp.txt;type=text/plain" localhost:8080/request_body
- 普通post请求:curl -d "method=searchone&module=seller&user_name=wb-liqiu&nickname=dd" -H"Host:fmp.view.lz.taobao.com" "10.235.160.141:8082/api.php"
- 模拟登录,传输cookie和参数:
- 登录:curl -c ./cookie_c.txt -F log=aaaa -F pwd=****** http://blog.51yip.com/wp-login.php
- 登录之后,可以使用-b参数,携带cookie -b ./cookie_c.txt http://www.***.com
- 伪造来源地址,有的网站会判断,请求来源地址
- curl -e http://localhost http://www.sina.com.cn
- 伪造cookie,防止302跳转页面不能下载
- wget --cookies=on --load-cookies=cookie.txt --keep-session-cookies --save-cookies=cookie.txt http://detail.tmall.com/item.htm?id=25139372969
- 使用proxy缓存
- curl -L -x127.0.0.1:13128 http://detail.tmall.com/item.htm?id=25823396605
命令事例详解
一、查看网页源码
直接在curl命令后加上网址,就可以看到网页源码。我们以网址www.sina.com为例(选择该网址,主要因为它的网页代码较短)
curl www.baidu.com
如果要把这个网页保存下来,可以使用-o参数,这就相当于使用wget命令了。
curl -o [文件名] www.baidu.com
二、自动跳转 有的网址是自动跳转的。使用-L参数,curl就会跳转到新的网址。curl -L http://item.taobao.com/item.htm?id=25823396605 键入上面的命令,结果就自动跳转为http://detail.tmall.com/item.htm?id=25823396605
三、显示头信息 -i 参数可以显示http response的头信息,连同网页代码一起。(-I 参数则是只显示http response的头信息。)
curl -i www.baidu.com

HTTP/1.1 200 OK Date: Fri, 28 Feb 2014 05:39:57 GMT Content-Type: text/html Transfer-Encoding: chunked Connection: Keep-Alive Vary: Accept-Encoding Set-Cookie: BAIDUID=0F251A658E427EBB7CBEB0C3F4A70FAE:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; d omain=.baidu.com Set-Cookie: BDSVRTM=0; path=/ Set-Cookie: H_PS_PSSID=4104_5231_1445_5139_5225_5378_5368_4261_4760_5400; path=/; domain=.baidu.com P3P: CP=" OTI DSP COR IVA OUR IND COM " Expires: Fri, 28 Feb 2014 05:39:45 GMT Cache-Control: private Server: BWS/1.1 BDPAGETYPE: 1 BDQID: 0xc3b306dca955703d BDUSERID: 0 <!DOCTYPE html>.....

四、显示通信过程 -v 参数可以显示一次http通信的整个过程,包括端口连接和http request头信息。命令:curl -v www.baidu.com

* About to connect() to www.baidu.com port 80
* Trying 115.239.210.26... connected
* Connected to www.baidu.com (115.239.210.26) port 80
> GET / HTTP/1.1
> User-Agent: curl/7.15.5 (x86_64-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5
> Host: www.baidu.com
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Fri, 28 Feb 2014 05:42:37 GMT
< Content-Type: text/html
< Transfer-Encoding: chunked
< Connection: Keep-Alive
< Vary: Accept-Encoding
< Set-Cookie: BAIDUID=442AD49501EF253AE71F2BAF3E0181FB:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
< Set-Cookie: BDSVRTM=0; path=/
< Set-Cookie: H_PS_PSSID=5228_1461_5187_5138_5225_5379_5368_4261_4760_5401_5286; path=/; domain=.baidu.com
< P3P: CP=" OTI DSP COR IVA OUR IND COM "
< Expires: Fri, 28 Feb 2014 05:41:43 GMT
< Cache-Control: private
< Server: BWS/1.1
< BDPAGETYPE: 1
< BDQID: 0x906950d16fb1e95d
< BDUSERID: 0
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Spee<!DOCTYPE html><!--STATUS OK--><html><head><meta http-equiv="content-type" content="text/html;charset=utf-8"><meta http-equiv
="X-UA-Compatible" content="IE=Edge"><link rel="dns-

如果你觉得上面的信息还不够,那么下面的命令可以查看更详细的通信过程: curl --trace output.txt www.baidu.com 或者 curl --trace-ascii output.txt www.baidu.com 运行后,请打开output.txt文件查看。
当然也有更狠的,查看页面跳转过程:curl -L -I --trace log http://item.taobao.com/item.htm?id=25823396605

HTTP/1.1 301 Moved Permanently Server: Tengine Date: Fri, 28 Feb 2014 06:16:01 GMT Content-Type: text/html;charset=GBK Content-Length: 0 Connection: close P3P: CP='CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR' Content-Language: zh-CN Accept-Ranges: bytes X-Varnish: 1206961665 Via: 1.1 varnish location: http://detail.tmall.com/item.htm?id=25823396605& X-Cache: MISS Cache-Control: max-age=3 HTTP/1.1 302 Found Server: Tengine Date: Fri, 28 Feb 2014 06:16:01 GMT Content-Type: text/html Content-Length: 260 Connection: keep-alive at_bucketid: sbucket_-1 X-Bucket-Id: -1 Location: http://jump.taobao.com/jump?target=http%3a%2f%2fdetail.tmall.com%2fitem.htm%3fid%3d25823396605%26%26tbpm%3d1 Cache-Control: HTTP/1.1 302 Found Date: Fri, 28 Feb 2014 06:16:01 GMT Content-Type: text/html Content-Length: 260 Connection: close Set-Cookie: _tb_token_=ktbzEwzFR6qy;domain=.taobao.com;Path=/;HttpOnly Set-Cookie: cookie2=6c6bc65b9e9a5159cff5b3d0cae4dfd9;domain=.taobao.com;Path=/;HttpOnly Set-Cookie: t=d768c73859b40e10ef81f7abd0824704;domain=.taobao.com;Expires=Thu, 29-May-2014 06:16:01 GMT;Path=/ P3P: CP='CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR' Location: http://pass.tmall.com/add?_tb_token_=ktbzEwzFR6qy&cookie2=6c6bc65b9e9a5159cff5b3d0cae4dfd9&t=d768c73859b40e10ef81f7abd0824704&target=http%3a%2f%2fdetail.tmall.com%2fitem.htm%3fid%3d25823396605%26%26tbpm%3d1&pacc=A_12nKe89qHIcAyauBtovg==&opi=110.75.118.230&tmsc=1393568161483632 HTTP/1.1 302 Found Date: Fri, 28 Feb 2014 06:16:01 GMT Content-Type: text/html Content-Length: 260 Connection: close P3P: CP='CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR' Set-Cookie: _tb_token_=ktbzEwzFR6qy;domain=.tmall.com;Path=/ Set-Cookie: cookie2=6c6bc65b9e9a5159cff5b3d0cae4dfd9;domain=.tmall.com;Path=/ Set-Cookie: t=d768c73859b40e10ef81f7abd0824704;domain=.tmall.com;Path=/ Location: http://detail.tmall.com/item.htm?id=25823396605&&tbpm=1 HTTP/1.1 302 Found Server: Tengine Date: Fri, 28 Feb 2014 06:16:01 GMT Content-Type: text/html Content-Length: 260 Connection: keep-alive at_bucketid: sbucket_-1 X-Bucket-Id: -1 Location: http://jump.taobao.com/jump?target=http%3a%2f%2fdetail.tmall.com%2fitem.htm%3fid%3d25823396605%26%26tbpm%3d2 Cache-Control: HTTP/1.1 302 Found Date: Fri, 28 Feb 2014 06:16:01 GMT Content-Type: text/html Content-Length: 260 Connection: close Set-Cookie: _tb_token_=GgU93fEjKGT4;domain=.taobao.com;Path=/;HttpOnly Set-Cookie: cookie2=ef3c440d74ff391de6b560da4ef8a5c9;domain=.taobao.com;Path=/;HttpOnly Set-Cookie: t=187a71d8df58caac2c4e08d40245c31f;domain=.taobao.com;Expires=Thu, 29-May-2014 06:16:01 GMT;Path=/ P3P: CP='CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR' Location: http://pass.tmall.com/add?_tb_token_=GgU93fEjKGT4&cookie2=ef3c440d74ff391de6b560da4ef8a5c9&t=187a71d8df58caac2c4e08d40245c31f&target=http%3a%2f%2fdetail.tmall.com%2fitem.htm%3fid%3d25823396605%26%26tbpm%3d2&pacc=Vo8f-VlYEYPJ6WE3iTX96Q==&opi=110.75.118.230&tmsc=1393568161501736 HTTP/1.1 302 Found Date: Fri, 28 Feb 2014 06:16:01 GMT Content-Type: text/html Content-Length: 260 Connection: close P3P: CP='CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR' Set-Cookie: _tb_token_=GgU93fEjKGT4;domain=.tmall.com;Path=/ Set-Cookie: cookie2=ef3c440d74ff391de6b560da4ef8a5c9;domain=.tmall.com;Path=/ Set-Cookie: t=187a71d8df58caac2c4e08d40245c31f;domain=.tmall.com;Path=/ Location: http://detail.tmall.com/item.htm?id=25823396605&&tbpm=2 HTTP/1.1 302 Found Server: Tengine Date: Fri, 28 Feb 2014 06:16:01 GMT Content-Type: text/html Content-Length: 260 Connection: keep-alive at_bucketid: sbucket_-1 X-Bucket-Id: -1 Location: http://jump.taobao.com/jump?target=http%3a%2f%2fdetail.tmall.com%2fitem.htm%3fid%3d25823396605%26%26tbpm%3d3 Cache-Control: HTTP/1.1 302 Found Date: Fri, 28 Feb 2014 06:16:01 GMT Content-Type: text/html Content-Length: 260 Connection: close Set-Cookie: _tb_token_=Uta86PoG6cWC;domain=.taobao.com;Path=/;HttpOnly Set-Cookie: cookie2=cd06dba05f2bf1124200861d0b8a151b;domain=.taobao.com;Path=/;HttpOnly Set-Cookie: t=618388c77aff03aec08309747a82f440;domain=.taobao.com;Expires=Thu, 29-May-2014 06:16:01 GMT;Path=/ P3P: CP='CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR' Location: http://pass.tmall.com/add?_tb_token_=Uta86PoG6cWC&cookie2=cd06dba05f2bf1124200861d0b8a151b&t=618388c77aff03aec08309747a82f440&target=http%3a%2f%2fdetail.tmall.com%2fitem.htm%3fid%3d25823396605%26%26tbpm%3d3&pacc=RptvnJCXDG4WtvFa0JIqoQ==&opi=110.75.118.230&tmsc=1393568161510003 HTTP/1.1 302 Found Date: Fri, 28 Feb 2014 06:16:01 GMT Content-Type: text/html Content-Length: 260 Connection: close P3P: CP='CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR' Set-Cookie: _tb_token_=Uta86PoG6cWC;domain=.tmall.com;Path=/ Set-Cookie: cookie2=cd06dba05f2bf1124200861d0b8a151b;domain=.tmall.com;Path=/ Set-Cookie: t=618388c77aff03aec08309747a82f440;domain=.tmall.com;Path=/ Location: http://detail.tmall.com/item.htm?id=25823396605&&tbpm=3 HTTP/1.1 200 OK Server: Tengine Date: Fri, 28 Feb 2014 06:16:01 GMT Content-Type: text/html;charset=GBK Connection: keep-alive Vary: Accept-Encoding at_bucketid: sbucket_-1 X-Bucket-Id: -1 Cache-Control: max-age=1 At_Autype: 4_103979994 At_Cat: item_50018599 X-Category: /cat/50008090 At_Nick: %E8%B6%8A%E5%BA%A6%E6%95%B0%E7%A0%81%E4%B8%93%E8%90%A5%E5%BA%97 At_Itemid: 25823396605 At_Isb: 1 At_Pgty: 2 At_Cat: 50018599 At_Brid: 272676782 At_Prid: 221236332 At_Autype: 0_103979994 At_Auid: 25823396605 Content-Language: zh-CN X-Cache: MISS TCP_MISS dirn:-2:-2 Via: wagbridge010207087016.cm3:8888

五、发送表单信息 发送表单信息有GET和POST两种方法。GET方法相对简单,只要把数据附在网址后面就行: curl baidu.com/form.cgi?data=xxx
POST方法必须把数据和网址分开,例如:curl -d "method=searchone&module=seller&user_name=wb-liqiu&nickname=dd" -H"Host:fmp.view.lz.taobao.com" "10.235.160.141:8082/api.php"
六、文件上传 假定文件上传的表单你可以用curl这样上传文件,命令:curl --form upload=@localfilename --form press=OK http://www.baidu.com
七、Referer字段 有时你需要在http request头信息中,提供一个referer字段,表示你是从哪里跳转过来的: curl --referer http://www.baidu.com http://www.baidu.com
八、User Agent字段 这个字段是用来表示客户端的设备信息。服务器有时会根据这个字段,针对不同设备,返回不同格式的网页,比如手机版和桌面版。 iPhone4的User Agent是: “Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7”。curl可以这样模拟: curl --user-agent "[User Agent]" [URL]
九、cookie 使用--cookie参数,可以让curl发送cookie。curl --cookie "name=xxx" www.baidu.com 至于具体的cookie的值,可以从http response头信息的Set-Cookie字段中得到。也可以例如:curl -c ./cookie.txt http://www.baidu.com
十、增加头信息 有时需要在http request之中,自行增加一个头信息。--header参数就可以起到这个作用。curl --header "xxx: xxxxxx" http://baidu.com
十一、HTTP认证 有些网域需要HTTP认证,这时curl需要用到--user参数:curl --user name:password baidu.com
<html> <meta http-equiv="refresh" content="0;url=http://www.baidu.com/"> </html>
十二、设置代理缓存 -x<ip:port>
curl -L -x127.0.0.1:13128 http://detail.tmall.com/item.htm?id=25823396605
http://www.cnblogs.com/liqiu/p/3200582.html
curl 命令详解(转)的更多相关文章
- Linux curl 命令详解
命令概要 该命令设计用于在没有用户交互的情况下工作. curl 是一个工具,用于传输来自服务器或者到服务器的数据.「向服务器传输数据或者获取来自服务器的数据」 可支持的协议有(DICT.FILE.FT ...
- windows curl命令详解
概述 Curl命令可以通过命令行的方式,执行Http请求.在Elasticsearch中有使用的场景,因此这里研究下如何在windows下执行curl命令. 软件下载 下载地址:https://cur ...
- 在线排错之curl命令详解
春回大地万物复苏,好久不来,向各位博友问好. 简介 cURL是一个利用URL语法在命令行下工作的文件传输工具,1997年首次发行.它支持文件上传和下载,所以是综合传输工具,但按传统,习惯称cURL为下 ...
- CURL命令详解
curl命令是一个强大的网络工具,它能通过http,ftp等方式下载.上传文件.其中curl远不止这些功能,大家可以通过阅读手册获取更多的信息,类似的工具还有wget. curl命令使用了libcur ...
- linux curl 命令详解,以及实例
linux curl是一个利用URL规则在命令行下工作的文件传输工具.它支持文件的上传和下载,所以是综合传输工具,但按传统,习惯称url为下载工具. 一,curl命令参数,有好多我没有用过,也不知道翻 ...
- Linux之curl命令详解
url命令是一个功能强大的网络工具,它能够通过http.ftp等方式下载文件,也能够上传文件.其实curl远不止前面所说的那些功能,大家可以通过man curl阅读手册页获取更多的信息.类似的工具还有 ...
- Linux curl命令详解
用途说明 curl命令是一个功能强大的网络工具,它能够通过http.ftp等方式下载文件,也能够上传文件.其实curl远不止前面所说的那些功能,大家可以通过man curl阅读手册页获取更多的信息.类 ...
- Linux Shell脚本编程--curl命令详解
用途说明 curl命令是一个功能强大的网络工具,它能够通过http.ftp等方式下载文件,也能够上传文件.其实curl远不止前面所说的那些功能,大家可以通过man curl阅读手册页获取更多的信息.类 ...
- curl 命令详解
curl命令是一个利用URL规则在命令行下工作的文件传输工具.它支持文件的上传和下载,所以是综合传输工具,但按传统,习惯称curl为下载工具.作为一款强力工具,curl支持包括HTTP.HTTPS.f ...
随机推荐
- TI推出SimpleLink低能耗蓝牙CC2541
TI推出SimpleLink低能耗蓝牙CC2541 日前,德州仪器 (TI) 宣布推出 SimpleLink™ 低能耗蓝牙 (Bluetooth®Low Energy) CC2541-Q1, 这是一款 ...
- POJ2599+POJ2082【最大矩形面积】
题目链接:http://poj.org/problem?id=2559 题目链接:http://poj.org/problem?id=2082 这一类题目的解法,不知自己闲着没事就做了两个. 果然压栈 ...
- Java NIO的性能
最近调研了一下mina和netty框架的性能,主要是想了解java nio在单机能支持多少长连接. 首先,mina的qq群有同学反映说单机支持3w长连接是没问题的 其次,http://amix.dk/ ...
- c++各类变量汇总
一.局部变量和全局变量: (1)局部变量:局部变量也叫自动变量,它声明在函数开始,生存于栈,它的生命随着函数的返回而结束. #include <stdio.h> int main(void ...
- ASP.NET MVC 4高级编程(第4版)
<ASP.NET MVC 4高级编程(第4版)> 基本信息 作者: (美)Jon Galloway Phil Haack Brad Wilson K. Scott All ...
- bzoj 3519: [Zjoi2014] 消棋子 题解
[序言]在大家怀疑的眼光下,我做了一个中午和半个下午.调了一个晚上的题目总算A了! [原题] 消棋子是一个有趣的游戏.游戏在一个r * c的棋盘上进行.棋盘的每一个格 子.要么是空,要么是一种颜色的棋 ...
- 读取数据表中第m条到第n条的数据,SQL语句怎么写?
原文:读取数据表中第m条到第n条的数据,SQL语句怎么写? 对于MySQL或者Oracle来说,如果实现从Table 表中取出第 m 条到第 n 条的记录操作,我们需要TOP函数(不是所有的数据库都支 ...
- C++习题 虚函数-计算图形面积
C++习题 虚函数-计算图形面积 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 122 Solved: 86 [cid=1143&pid=6 ...
- Knockout应用开发指南 第十章:更多信息(完结篇)
原文:Knockout应用开发指南 第十章:更多信息(完结篇) 1 浏览器支持 Knockout在如下浏览器通过测试: Mozilla Firefox 2.0+(最新测试版本:3.6.8) Goo ...
- mini2440裸试验—计算器(LCD显示,触摸屏突破)
关于Pait_Bmp(x0, y0, x, y, BMPaddr);函数 像素图在屏幕左上角为(0,0),Pait_Bmp中的x0.y0分别像素点初始位置,x,y为BMP图片的X,Y的大小.BMPad ...