Curl 常用命令

引用

http://www.gbin1.com/technology/javautilities/20120610curl-introduction/
http://curl.haxx.se/download.html

1. 读取URL页面
以下命令用来读取一个URL地址内容,如下:
curl http://www.gbin1.com


读取https协议:
curl https://www.gbin1.com


读取一个web地址并且保存到一个文件中:
curl -o gbin1.html http://www.gbin1.com/


读取一个需要HTTP Basic认证的页面:
curl -u username:password http://www.gbin1.com/


如果页面有重定向,注意curl不会自动处理,你需要添加参数,如下:
curl -L http://www.gbin1.com/404


2. 使用参数读取URL页面
你可以使用参数来取得页面内容:
curl http://www.gbin1.com/bloghome.html?firstentry=15


同时如果你需要下载所有页面,你可以使用正则表达式:
curl http://www.gbin1.com/bloghome.html?firstentry=[1-15] 


3. 读取document信息
你可以读取头信息:
curl --head http://www.gbin1.com/


支持读取其它类型文件,例如,图片:
curl --head http://www.gbin1.com/gbin1/themes/gbin1_2column_bloghome/images/logo.png


将头信息dump保存到文件:
curl --dump-header headers.txt http://www.gbin1.com/ 


4. 处理FTP
curl ftp://username:[email protected]

获取指定目录,如下:
curl ftp://username:[email protected]/technology/


上传文件:
curl -T uploadfilename -u username:password ftp://gbin1.com/somefilename

被上传的文件uploadfilename将会被上传到远端并且改名为somefilename。

5. 使用POST方法来获取页面
如果你需要使用POST方法递交表单,使用curl也非常简单,例如你有如下表单:
<form method="POST" action="login.php"><input type=text name="username"><input type=text name="password"><input type=submit name="submit" value="login"> </form>
你可以使用如下命令:
curl -d "username=terry&password=123&submit=login"             www.gbin1.com/login.php


如果你希望访问登录后才可以看到的页面,你可以配合使用cookie来实现,如下:
curl --cookie-jar "cookie.txt" -d "username=terry&password=123" http://www.gbin1.com/login.php
curl -b "cookie.txt" http://www.gbin1.com/showusers.php

以上代码你可以看到你登录后,将能够访问showusers.php这个页面。

6. Referer和User Agent
如果你希望能够模拟生成referer字段,你可以使用-e参数,如下:
curl -e http://www.google.com  http://www.gbin1.com/

你将看到所有来源会参考google.com

当然,你也可以模拟不同的用户端代理(user-ageng)字段,如下:
curl -A "Mozilla/5.0(compatible; MSIE 7.01; Windows NT 5.0)"          http://www.gbin1.com

使用以上代码我们可以模拟Mozilla的浏览器访问服务器。

猜你喜欢

转载自robinwu.iteye.com/blog/1559212
今日推荐