curl 用于Linux命令行中去访问网页

curl 用于Linux命令行中去访问网页
[root@daixuan ~]# curl www.aminglinux.com                              curl+网址查看网站源代码
[root@daixuan ~]# curl -I www.lishiming.net                               -I省略掉源代码
HTTP/1.1 301 Moved Permanently       301表示当访问域名www.lishiming.net会自动跳转到http://www.apelearn.com/bbs/forum.php    
Server: nginx/1.0.15
Date: Sat, 21 Nov 2015 06:43:39 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://www.apelearn.com/bbs/forum.php                

状态码
200   是正常的
301 302 在服务端设定了跳转

404    
502 
503
[root@daixuan ~]# ping www.baidu.com
PING www.a.shifen.com (103.235.46.39) 56(84) bytes of data.
64 bytes from 103.235.46.39: icmp_seq=1 ttl=45 time=207 ms
[root@daixuan ~]# vim /etc/hosts                                                          在临时测试域名www.baidu.com为192.168.101.230的情况下
192.168.101.230 www.baidu.com
[root@daixuan ~]# ping www.baidu.com
PING www.baidu.com (192.168.101.230) 56(84) bytes of data.
64 bytes from www.baidu.com (192.168.101.230): icmp_seq=1 ttl=64 time=3.08 ms
[root@daixuan ~]# curl www.baidu.com
curl: (7) couldn't connect to host           
[root@daixuan ~]# curl -x 103.235.46.39:80 www.baidu.com -I     -x代理设置,指定IP和端口仍然何以正常访问www.baidu.com,不加-x不能正常访问
HTTP/1.1 200 OK
Date: Sat, 21 Nov 2015 07:01:16 GMT
Server: Apache
P3P: CP=" OTI DSP COR IVA OUR IND COM "
P3P: CP=" OTI DSP COR IVA OUR IND COM "
[root@daixuan ~]# curl -Iv www.baidu.com                                   -Iv看出访问过程
* About to connect() to www.baidu.com port 80 (#0)
*   Trying 103.235.46.39... connected                                            解析到IP并连接
* Connected to www.baidu.com (103.235.46.39) port 80 (#0)       User-Agent是浏览器的表示,谷歌,百度浏览器都有自己的标识
> HEAD / HTTP/1.1
User-Agent: curl/7.19.7 (i386-redhat-linux-gnu) libcurl/7.19.7 NSS/3.19.1 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: www.baidu.com
> Accept: */*
>
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< Date: Sat, 21 Nov 2015 07:23:50 GMT
Date: Sat, 21 Nov 2015 07:23:50 GMT
< Content-Type: text/html; charset=utf-8
Content-Type: text/html; charset=utf-8
< Connection: Keep-Alive
Connection: Keep-Alive
< Vary: Accept-Encoding
Vary: Accept-Encoding
[root@daixuan ~]# cul -u username:password http://www.qqq.com     使用用户名和密码来访问网站
[root@daixuan ~]# curl -O +图片网址                                                  下载图片
Dload  Upload   Total   Spent    Left  Speed
100  3706  100  3706    0     0   4388      0 --:--:-- --:--:-- --:--:--  113k
[root@daixuan ~]# curl -o baidu.png +图片的地址                              指定下载名称下载图片
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  3706  100  3706    0     0   8591      0 --:--:-- --:--:-- --:--:-- 80565
[root@daixuan ~]# ls
111      1234.txt      1.t       #1.txt.bak  3.txt            baidu.png              install.log         模板  桌面
1112     1234.txt.bak  1.tar     1.txt.bak   3.txt.bak        bd_logo1_31bdc765.png 

curl在请求一个网页的时候,可以指定user_agent,如何做?


curl -A 'aaa' www.aaa.com


你可能感兴趣的:(curl)