`
haohappy2
  • 浏览: 316325 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Curl Command

 
阅读更多
curl是一种命令行工具,作用是发出网络请求,然后得到和提取数据,显示在"标准输出"(stdout)上面。@舍得Share

它支持多种协议,下面举例讲解如何将它用于网站开发。

一、查看网页源码

直接在curl命令后加上网址,就可以看到网页源码。我们以网址www.sina.com为例(选择该网址,主要因为它的网页代码较短):

  curl www.tvbs.cc

  <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>

The document has moved <a href="http://www.tvbs.cc/">here.


</body></html>

 

如果要把这个网页保存下来,可以使用-o参数,这就相当于使用wget命令了。

  curl -o [文件名] www.tvbs.cc

二、自动跳转

有的网址是自动跳转的。使用-L参数,curl就会跳转到新的网址。

  curl -L www.tvbs.cc

键入上面的命令,结果就自动跳转为www.tvbs.cc。

三、显示头信息

-i参数可以显示http response的头信息,连同网页代码一起。

  curl -i www.tvbs.cc

  HTTP/1.0 301 Moved Permanently
Date: Sat, 03 Sep 2011 23:44:10 GMT
Server: Apache/2.0.54 (Unix)
Location: http://www.sina.com.cn/
Cache-Control: max-age=3600
Expires: Sun, 04 Sep 2011 00:44:10 GMT
Vary: Accept-Encoding
Content-Length: 231
Content-Type: text/html; charset=iso-8859-1
Age: 3239
X-Cache: HIT from sh201-9.sina.com.cn
Connection: close

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>

The document has moved <a href="http://www.sina.com.cn/">here.


</body></html>

 

-I参数则是只显示http response的头信息。

四、显示通信过程

-v参数可以显示一次http通信的整个过程,包括端口连接和http request头信息。

  curl -v www.tvbs.cc

  * About to connect() to www.sina.com port 80 (#0)
* Trying 61.172.201.195... connected
* Connected to www.sina.com (61.172.201.195) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.21.3 (i686-pc-linux-gnu) libcurl/7.21.3 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18
> Host: www.sina.com
> Accept: */*
>
* HTTP 1.0, assume close after body
< HTTP/1.0 301 Moved Permanently
< Date: Sun, 04 Sep 2011 00:42:39 GMT
< Server: Apache/2.0.54 (Unix)
< Location: http://www.sina.com.cn/
< Cache-Control: max-age=3600
< Expires: Sun, 04 Sep 2011 01:42:39 GMT
< Vary: Accept-Encoding
< Content-Length: 231
< Content-Type: text/html; charset=iso-8859-1
< X-Cache: MISS from sh201-19.sina.com.cn
< Connection: close
<
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>

The document has moved <a href="http://www.sina.com.cn/">here.


</body></html>
* Closing connection #0

 

如果你觉得上面的信息还不够,那么下面的命令可以查看更详细的通信过程。

  curl --trace output.txt www.tvbs.cc

或者

  curl --trace-ascii output.txt www.tvbs.cc

运行后,请打开output.txt文件查看。

五、发送表单信息

发送表单信息有GET和POST两种方法。GET方法相对简单,只要把数据附在网址后面就行。

  curl tvbs.cc/form.cgi?data=xxx

POST方法必须把数据和网址分开,curl就要用到--data参数。

  curl --data "data=xxx" tvbs.cc/form.cgi

如果你的数据没有经过表单编码,还可以让curl为你编码,参数是--data-urlencode。

  curl --data-urlencode "date=April 1" tvbs.cc/form.cgi

六、文件上传

假定文件上传的表单是下面这样:

  <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字段

有时你需要在http request头信息中,提供一个referer字段,表示你是从哪里跳转过来的。

  curl --referer http://www.tvbs.cc

http://www.tvbs.cc

八、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。

  curl --cookie "name=xxx" www.tvbs.cc

至于具体的cookie的值,可以从http response头信息的Set-Cookie字段中得到。

十、增加头信息

有时需要在http request之中,自行增加一个头信息。--header参数就可以起到这个作用。

  curl --header "xxx: xxxxxx" http://tvbs.cc

十一、HTTP认证

有些网域需要HTTP认证,这时curl需要用到--user参数。

  curl --user name:password tvbs.cc

分享到:
评论

相关推荐

    IRI2012电离层模型 matlab代码

    As stated, the query is made using the command curl in an operating system terminal. This program is built-in to Unix (and Mac I believe) but not Windows. Windows users can download cURL from ...

    linux下提示bash-command not found.docx

    linux下提示bash-command not found.docx

    cognitocurl:with使用Cognito授权令牌轻松签署对API网关的curl调用

    认知卷曲 :locked_with_pen: ... $ cognitocurl --cognitoclient XXX --userpool YYY --run "full curl command here" 要么 $ cognitocurl --hostedui hosteduisetupfilename.json --run "full curl comma

    curlify:从 cURL 命令行参数生成节点 HTTP(S) 请求

    // generate a request method from this cURL command string var request = curlify ( 'curl -X POST -H "content-type: application/json" -d \'{"foo": "bar"}\' http://example.com' ) // make an ...

    curl for win32

    curl for win32,Curl is a command line tool for transferring data specified with URLsyntax.

    curl-7.67.0.zip

    command line tool and library for transferring data with URLs Supports... DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, ...

    curl-7.42.0.tar.bz2

    curl is a command line tool and library for transferring data with URL syntax, supporting DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB,...

    curl-7.41.0.tar.bz2

    curl is a command line tool and library for transferring data with URL syntax, supporting DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB,...

    everything-curl.pdf

    Everything curl is an extensive guide to everything there is to know about curl, the project, the command-line tool, the library, how everything started and how it came to be what it is today. How we ...

    基于window系统的Curl命令行接口调试工具

    Curl(CommandLine Uniform Resource Locator)翻译为:命令行统一资源定位器, Curl是一个利用URL语法在命令行方式下工作的文件传输工具. 可以简单理解为: 在命令行环境下访问url链接地址的工具(简记为:cURL或Curl或...

    curl-7.59.0

    curl is used in command lines or scripts to transfer data. It is also used in cars, television sets, routers, printers, audio equipment, mobile phones, tablets, settop boxes, media players and is the ...

    curl-curl-7_24_0.tar.gz

    curl-7_24_0.tar.gz A command line tool and library for transferring data with URL syntax, supporting HTTP, HTTPS, FTP, FTPS, GOPHER, TFTP, SCP, SFTP, SMB, TELNET, DICT, LDAP, LDAPS, FILE, IMAP, SMTP, ...

    vscode-restclient:用于Visual Studio Code的REST客户端扩展

    在编辑器中发送cURL命令并将HTTP请求复制为cURL command 自动保存并查看/清除请求历史记录 在同一文件中组织多个请求(以###分隔符分隔) 直接在窗格中查看图像响应 将原始响应和响应正文仅保存到本地磁盘 折叠并...

    gitconfig:个人配置和东西

    sudo apt-get install wget curl command-not-found vim 运行deploy.sh 用您的名称/电子邮件更改~/.gitconfig并提交更改 您可能还会喜欢一些额外的东西: ack-grep在VIM中用作grep命令。 只需安装ack-grep软件包 ...

    curl-builder:卷曲命令生成器库

    curl-builder 是一个 curl 命令生成器,它可以根据 PSR-7 服务器请求自动生成 curl 命令,也可以通过指定选项和 URL 手动生成 curl 命令。 安装 composer require alexkart/curl-builder 例子 从 PSR-7 请求生成 ...

    windows版curl

    -e, --execute=COMMAND execute a `.wgetrc' command. Logging and input file: -o, --output-file=FILE log messages to FILE. -a, --append-output=FILE append messages to FILE. -d, --debug print ...

    curl-7.69.1.zip

    curl is used in command lines or scripts to transfer data. It is also used in cars, television sets, routers, printers, audio equipment, mobile phones, tablets, settop boxes, media players and is the ...

    curl-7.59.0.zip

    Curl is a command-line tool for transferring data specified with URL syntax. Find out how to use curl by reading [the curl.1 man page](https://curl.haxx.se/docs/manpage.html) or [the MANUAL document]...

    http2curl:将Golang的http.Request转换为CURL命令行

    http2curl :triangular_ruler: 将Golang的http.Request转换为CURL命令行 要执行相反的操作,请检出 。... Set ( "Content-Type" , "application/json" )command , _ := http2curl . GetCurlCommand ( req )fmt . Printl

    cUrl-7.39.0

    CURLOPT_COOKIELIST: Added "RELOAD" command build: Added WinIDN build configuration options to Visual Studio projects ssh: improve key file search SSL: public key pinning. Use CURLOPT_PINNEDPUBLICKEY ...

Global site tag (gtag.js) - Google Analytics