curl命令总结

在开发客户端的时候,需要对请求的正确性以及返回数据格式进行验证,每次调试都去发送请求过于麻烦。于是,我找到了一个方便偷懒的工具——curl。curl是一个向服务器传输数据的工具,支持http、https、ftp、ftps、scp、sftp、tftp、telnet等协议。以下总结了一些针对http的常用方法:
命令使用如下:
一、查看网页源代码
1、直接在curl命令后加上网址,就可以看到网页源码。我们以网址www.baidu.com为例
[html]  view plain  copy
  1. curl www.baidu.com  
2、如果要把这个网页保存下来,可以使用-o参数,这就相当于使用wget命令了。
[html]  view plain  copy
  1. curl -o [文件名] http://www.baidu.com  
  2. 或者  
  3. curl  http://www.baidu.com > [文件名]  
  4. eg:curl -o /home/baidu.html http://www.baidu.com  
  5.    curl http://www.baidu.com > /home/baidu.html  
二、发送请求
1、GET请求
默认直接请求一个url就是发出一个get请求,参数的话直接拼接在url里就好了,如
[html]  view plain  copy
  1. curl http://www.baidu.com/s?wd=curl  
上述请求会上百度发起一条查询请求,参数是wd=curl
2、POST请求
普通post请求:
[html]  view plain  copy
  1. curl -d "name=test&page=1" http://www.baidu.com  
  2. -d 参数指定表单以POST的形式执行。  
通过脚本发送post请求,顺便附带文本数据,比如通过"浏览"选择本地的card.txt并上传发送post请求
[html]  view plain  copy
  1. curl  -F "blob[email protected];type=text/plain"  "http://172.16.102.208:8089/wiapi/score?leaderboard_id=7778a8143f111272&score=40&app_key=8d49f16fe034b98b&_test_user=test01"     
注意:其中-F 为带文件的形式发送post请求,blob为文本框中的name元素对应的属性值。<type="text" name="blob">

3. POST json请求
curl -H "Content-Type:application/json" -X POST --data '{"key": "mapper.osr.Gerrit.bo.changecomments"}'  http://xxx

三、HTTP方法
curl默认的HTTP方法是GET,使用-X参数可以支持其他动词。
[html]  view plain  copy
  1. curl -X POST www.example.com  
  2. curl -X DELETE www.example.com  
 四、自动跳转
 有的网址是自动跳转的。使用-L参数,curl就会跳转到新的网址。
[html]  view plain  copy
  1. curl -L http://item.taobao.com/item.htm?id=25823396605  
 键入上面的命令,结果就自动跳转为http://detail.tmall.com/item.htm?id=25823396605
五、显示头信息
-i 参数可以显示http response的头信息,连同网页代码一起。(-I参数则是只显示http response的头信息)
[html]  view plain  copy
  1. curl -i http://www.baidu.com  
  2. curl -I http://www.baidu.com  
六、显示通信过程
-v参数可以显示一次http通信的整个过程,包括端口连接和http request头信息
[html]  view plain  copy
  1. curl -v www.baidu.com  
  2.   
  3. * About to connect() to www.baidu.com port 80  
  4. *   Trying 115.239.210.26... connected  
  5. * Connected to www.baidu.com (115.239.210.26) port 80  
  6. > GET / HTTP/1.1  
  7. > 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  
  8. > Host: www.baidu.com  
  9. > Accept: */*  
  10. >  
  11. < HTTP/1.1 200 OK  
  12. < Date: Fri, 28 Feb 2014 05:42:37 GMT  
  13. < Content-Type: text/html  
  14. < Transfer-Encoding: chunked  
  15. < Connection: Keep-Alive  
  16. < Vary: Accept-Encoding  
  17. < Set-Cookie: BAIDUID=442AD49501EF253AE71F2BAF3E0181FB:FG=1expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647path=/; domain=.baidu.com  
  18. < Set-Cookie: BDSVRTM=0path=/  
  19. < Set-Cookie: H_PS_PSSID=5228_1461_5187_5138_5225_5379_5368_4261_4760_5401_5286path=/; domain=.baidu.com  
  20. < P3P: CP=" OTI DSP COR IVA OUR IND COM "  
  21. < Expires: Fri, 28 Feb 2014 05:41:43 GMT  
  22. < Cache-Control: private  
  23. < Server: BWS/1.1  
  24. < BDPAGETYPE: 1  
  25. < BDQID: 0x906950d16fb1e95d  
  26. < BDUSERID: 0  
  27.   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current  
  28.                                  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  
  29. ="X-UA-Compatible" content="IE=Edge">  
如果你觉得上面的信息还不够,那么下面的命令可以查看更详细的通信过程。
[html]  view plain  copy
  1. curl --trace output.txt www.baidu.com  
七、Referer字段
有时你需要在http request头信息中,提供一个referer字段,表示你是从哪里跳转过来的:
[html]  view plain  copy
  1. 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。
[html]  view plain  copy
  1. curl --cookie "name=xxx;age=12" www.baidu.com  
至于具体的cookie的值,可以从http response头信息的Set-Cookie字段中得到。也可以例如:
[html]  view plain  copy
  1. curl -c ./cookie.txt http://www.baidu.com  
十、增加头信息
有时需要在http request之中,自行增加一个头信息。--header参数就可以起到这个作用。
[html]  view plain  copy
  1. curl --header "xxx: xxxxxx" http://baidu.com  
  2. 或者
  3. curl -H "aa:ss" -H "cc:11" http://baidu.com  
十一、HTTP认证
有些网域需要HTTP认证,这时curl需要用到--user参数:
[html]  view plain  copy
  1. curl --user name:password baidu.com  
[html]  view plain  copy






猜你喜欢

转载自blog.csdn.net/zhuchunyan_aijia/article/details/80103400