Linux:curl的使用

来源:互联网 发布:mplayerx for mac破解 编辑:程序博客网 时间:2024/06/09 17:22

Linux:curl的使用

curl  is  a  tool  to  transfer data from or to a server, using one of the supported protocols (HTTP, HTTPS, FTP, FTPS, TFTP, DICT, TELNET, LDAP or FILE). The command is designed to work without user interaction.

curl支持多种协议。


使用curl发送一个标准的http请求报文,POST方式,携带数据

【Sample 1】

curl -X POST -d @req.json http://127.0.0.1:8100/

实际发出去的HTTP报文:

POST / HTTP/1.1^MUser-Agent: curl/7.15.5 (x86_64-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.8 libidn/0.6.5^MHost: 127.0.0.1:8100^MAccept: */*^MContent-Length: 60^MContent-Type: application/x-www-form-urlencoded^M^M{   "id":"123456789",   "action":"query",   "username":"test1280"}

针对上述示例有以下解释:

-X/--request <command>(HTTP) Specifies a custom request method to use when communicating with the HTTP server.The specified request will be used instead of the method otherwise used (which defaults to GET).

代表的是使用HTTP-POST方法发起请求,默认的是GET。

-d是用来指明要发送出去的数据。

-d/--data <data>(HTTP)  Sends  the specified data in a POST request to the HTTP server, in a way that can emulate as if a user has filled in a HTML form and pressed the submit button.The  data is  expected  to  be "url-encoded".This will cause curl to pass the data to the server using the content-type application/x-www-form-urlen-coded.If this option is used more than once on the same command line,  the  data  pieces  specified  will  be  merged together  with  a  separating  &-letter.Thus,  using  ’-d  name=daniel  -d  skill=lousy’  would  generate  a  post  chunk  that looks like ’name=daniel&skill=lousy’.If you start the data with the letter @, the rest should be a file name to read the data from, or - if you want curl to read the  data  from  stdin.The contents of the file must already be url-encoded.Multiple files can also be specified.Posting data from a file named ’foobar’ would thus be done with --data @foobar".To post data purely binary, you should instead use the --data-binary option.

我们例子中-d后面带有@req.json,其中,req.json内容如下:

{    "id":"123456789",    "action":"query",    "username":"test1280"}

使用@开头可以将一个文件中的内容作为报文的正文部分发出去。

我们应该注意到一点:

被期待的数据是url-encoded的。

我发送的是一个json数据,Content-Type应该是json/text类型的。

可以使用-H选项来指定头。

-H--header
(HTTP) Extra header to use when getting a web page.You may specify any number of extra headers.Note that if you should add a custom header that has the same name as one of the internal ones curl would use, your externally set header will be used instead of the internal one.This option can be used multiple times to add/replace/remove multiple headers.
curl -X POST -d @req.json -H 'Content-Type:json/text' http://127.0.0.1:8100/curl -X POST -d @req.json --header 'Content-Type:json/text' http://127.0.0.1:8100/

发出去的实际的报文如下:

POST / HTTP/1.1^MUser-Agent: curl/7.15.5 (x86_64-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.8 libidn/0.6.5^MHost: 127.0.0.1:8100^MAccept: */*^MContent-Type:json/text^MContent-Length: 60^M^M{   "id":"123456789",   "action":"query",   "username":"test1280"}

当然,你可以使用-H(–header)选项来replace多个头部参数。

注意一点,在同一命令行中可以使用多个-H来指定。

curl -X POST -d @./20170522/req.json -H 'Content-Type:json/text' -H 'User-Agent:test1280,hello world!' http://127.0.0.1:8100/

实际发出去的报文是:

POST / HTTP/1.1^MHost: 127.0.0.1:8100^MAccept: */*^MContent-Type:json/text^MUser-Agent:test1280,hello world!^MContent-Length: 60^M^M{   "id":"123456789",   "action":"query",   "username":"test1280"}

其实-H(–header)的作用是:

添加一个key-value对到HttpHeader,如果key已经存在,那就replace它,如果key没有存在,那就填进去。

【Summary 1】

发送一个报文大体上就是上面的几个参数了:

1.-X/–request POST指定使用POST方式发起请求(默认为GET);

2.-d/–data @xxx.ext指定HTTP报文携带的数据;

3.-H/–header来自定义HTTP-Header信息;


使用curl请求一个网页

curl www.baidu.com

这样子的话会直接将得到的响应的正文部分(即实际的网页)输出到终端。

可以使用-o或者重定向来将其输出至指定的文件。

curl www.baidu.com -o index.extcurl www.baidu.com >> index.ext

注:当index.ext中有乱码时,我们可以先使用file命令看看他是啥编码的,然后按照我们的需要使用iconv命令来转换到指定的格式即可正常查看。


使用curl请求一个网页,同时假装是从某个网站跳过来的

其实就是referer。

这个referer是HTTP-Header中的一部分,此处我们可以使用-e/–referer指定,当然,也可以使用上面提到的-H来指定。

curl -e "www.baidu.com" http://127.0.0.1/8100curl --referer "www.baidu.com" http://127.0.0.1/8100curl -H 'Referer:www.baidu.com' http://127.0.0.1:8100/

curl对cookie的处理

保留cookie到指定文件(cookie.ext)

curl -c cookie.ext www.baidu.comcurl --cookie-jar cookie.ext www.baidu.com

cookie.ext内容是:

# Netscape HTTP Cookie File# http://curl.haxx.se/docs/http-cookies.html# This file was generated by libcurl! Edit at your own risk..baidu.com  TRUE    /   FALSE   1495530085  BDORZ   27315

使用cookie.ext访问之前的网站

curl -b cookie.ext www.baidu.comcurl --cookie cookie.ext www.baidu.com

模拟一些指定的浏览器来访问目的网站

其实就是修改User-Agent,这个是在HTTP-Header中的。

类似referer,我们可以单独用-H来指定,也可以使用-A来指定。

curl -A 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.0)' http://localhost:8100/curl --user-agent 'User-Agent=Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.0)' http://localhost:8100/

资料参考:

0.http://www.cnblogs.com/gbyukg/p/3326825.html
1.https://linux.cn/article-4957-1.html
2.http://www.aiezu.com/system/linux/linux_curl_syntax.html
3.http://www.linuxdiyf.com/linux/2800.html
4.http://man.linuxde.net/curl
5.http://blog.csdn.net/privatemain/article/details/9787985