Linux值curl基本用法

2023-05-06 03:56:16

 

北风中独行的蜗牛zhuanlan.zhihu.com/jump_10

curl 是一个很强大的命令行工具。你可以把 CURL 想象成一个精简的命令行网页浏览器。它支持几乎你能想到的所有协议,可以交互访问几乎所有在线内容。唯一和浏览器不同的是,cURL 不会渲染接收到的相应信息。

curl和wget类似也支持上传下载等感觉比wget更强大,但我觉得用途方面更偏重于模拟网络请求,而下载方面我更喜欢用wget,curl的用法也和wget类似!查看源码,直接curl 网址,源码就会打印在命令行上:
curl www.baidu.com
也可以保存源码 用curl -O 文件名 url:
curl -O baidu.txt wwww.baidu.com

这个和wget类似

wget -O baidu1 www.baidu.com
显示网页头部信息 用-i,当然也会把网页信息显示出来
[root@VM_0_11_centos training]# curl -i www.baidu.com HTTP/1.1 200 OK Accept-Ranges: bytes Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform Connection: keep-alive Content-Length: 2381 Content-Type: text/html Date: Thu, 02 Apr 2020 02:14:33 GMT Etag: "588604c8-94d" Last-Modified: Mon, 23 Jan 2017 13:27:36 GMT Pragma: no-cache Server: bfe/1.0.8.18 Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/ <!DOCTYPE html> xxx </html>
参数 -v可以显示通信的过程:
[root@VM_0_11_centos training]# curl -v www.baidu.com * About to connect() to www.baidu.com port 80 (#0) * Trying 180.101.49.11... * Connected to www.baidu.com (180.101.49.11) port 80 (#0) > GET / HTTP/1.1 > User-Agent: curl/7.29.0 > Host: www.baidu.com > Accept: */* > < HTTP/1.1 200 OK < Accept-Ranges: bytes < Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform < Connection: keep-alive < Content-Length: 2381 < Content-Type: text/html < Date: Thu, 02 Apr 2020 02:16:36 GMT < Etag: "588604c8-94d" < Last-Modified: Mon, 23 Jan 2017 13:27:36 GMT < Pragma: no-cache < Server: bfe/1.0.8.18 < Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/ <

更详细的通信信息可以用 参数 --trance 文件名 url,具体信息保存到单独的文件中

[root@VM_0_11_centos training]# curl --trace info.txt www.baidu.com
htpp的动词,例如GET POST,PUT,DELETE等,需要参数 -X
curl默认的是get请求,如果发送POSt请求 curl -X POST www.baidu.com

发送表单的时候,GET很简单 只需要把数据拼接到url后面就行

curl www.baidu.com?data=xxx&data1=xxx

POST也不难

curl -X POST --data "data=xxx" example.com/form.cgi

POST发送请求的数据体可以用-d

$ curl -dlogin=emma&password=123-X POST https://google.com/login 或者 $ curl -d login=emma -d password=123 -X POST https://google.com/login

使用-d参数以后,HTTP 请求会自动加上标头Content-Type : application/x-www-form-urlencoded。并且会自动将请求转为 POST 方法,因此可以省略-X POST。

-d参数可以读取本地文本文件的数据,向服务器发送。
$ curl -d @data.txt https://google.com/login

上面命令读取data.txt文件的内容,作为数据体向服务器发送。

文件上传: 假定文件上传的表单是下面这样:
<form method="POST" enctype=multipart/form-data action="upload.cgi">     <input type=file name=upload>     <input type=submit name=press value="OK">   </form>

curl上传就应该是:

curl --form upload=@localfilename --form press=OK [URL]
--referer参数表示的是你从哪个页面来的
[root@VM_0_11_centos training]# curl --referer www.baidu.com www.baidu.com
User Agent字段,这个字段表示的是客户端设备的信息,服务器可能会根据这个User Agent字段来判断是手机还是电脑
curl --user-agent " xx" url 比如IPhone Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5 curl --user-agent "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5" www.baidu.com

--user-agent 可以用-A或者-H来替代

--cookie参数,使用--cookie可以携带cookie信息
curl --cookie "name=xxx" URL `-c cookie-file`可以保存服务器返回的cookie到文件, `-b cookie-file`可以使用这个文件作为cookie信息,进行后续的请求。
增加头部信息 --header
curl --header "Content-Type:application/json" http://example.com

参考:

http://www.ruanyifeng.com/blog/2011/09/curl.htmlhttp://www.ruanyifeng.com/blog/2019/09/curl-reference.html


以上就是关于《Linux值curl基本用法》的全部内容,本文网址:https://www.7ca.cn/baike/22810.shtml,如对您有帮助可以分享给好友,谢谢。
标签:
声明

排行榜