對接第三方軟件的時候 ,發(fā)現(xiàn)請求示例中 《curl -X PUT -H》這種格式
對接第三方軟件的時候 ,發(fā)現(xiàn)請求示例中 《curl -X PUT -H》這種格式
1:干什么的?
curl是一種命令行工具,作用是發(fā)出網(wǎng)絡(luò)請求,然后得到和提取數(shù)據(jù),顯示在"標準輸出"(stdout)上面。它的名字就是客戶端(client)的 URL 工具的意思
2:哪些操作系統(tǒng)支持這個命令?
linux 和 windows
在windows環(huán)境下的請求結(jié)果
3:官方文檔
https://catonmat.net/cookbooks/curl
curl的初級用法,學習阮一峰老的博客
4、自動跳轉(zhuǎn)
有的網(wǎng)址是自動跳轉(zhuǎn)的。使用`-L`參數(shù),curl就會跳轉(zhuǎn)到新的網(wǎng)址。
$ curl -L www.sina.com
鍵入上面的命令,結(jié)果就自動跳轉(zhuǎn)為www.sina.com.cn。
5、顯示頭信息
`-i`參數(shù)可以顯示http response的頭信息,連同網(wǎng)頁代碼一起。
$ curl -i www.sina.com
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
Moved Permanently
The document has moved here.
`-I`參數(shù)則是只顯示http response的頭信息。
6、顯示通信過程
`-v`參數(shù)可以顯示一次http通信的整個過程,包括端口連接和http request頭信息。
$ curl -v www.sina.com
* 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
<
Moved Permanently
The document has moved here.
* Closing connection #0
如果你覺得上面的信息還不夠,那么下面的命令可以查看更詳細的通信過程。
$ curl --trace output.txt www.sina.com
或者
$ curl --trace-ascii output.txt www.sina.com
運行后,請打開output.txt文件查看。
7、發(fā)送表單信息
發(fā)送表單信息有GET和POST兩種方法。GET方法相對簡單,只要把數(shù)據(jù)附在網(wǎng)址后面就行。
$ curl example.com/form.cgi?data=xxx
POST方法必須把數(shù)據(jù)和網(wǎng)址分開,curl就要用到--data參數(shù)。
$ curl -X POST --data "data=xxx" example.com/form.cgi
如果你的數(shù)據(jù)沒有經(jīng)過表單編碼,還可以讓curl為你編碼,參數(shù)是`--data-urlencode`。
$ curl -X POST--data-urlencode "date=April 1" example.com/form.cgi
8、HTTP動詞
curl默認的HTTP動詞是GET,使用`-X`參數(shù)可以支持其他動詞。
$ curl -X POST www.example.com
$ curl -X DELETE www.example.com
9、文件上傳
假定文件上傳的表單是下面這樣:
你可以用curl這樣上傳文件:
$ curl --form?upload=@localfilename?--form press=OK [URL]
10、Referer字段
有時你需要在http request頭信息中,提供一個referer字段,表示你是從哪里跳轉(zhuǎn)過來的。
$ curl --referer http://www.example.com http://www.example.com
11、User Agent字段
這個字段是用來表示客戶端的設(shè)備信息。服務(wù)器有時會根據(jù)這個字段,針對不同設(shè)備,返回不同格式的網(wǎng)頁,比如手機版和桌面版。
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]
12、cookie
使用`--cookie`參數(shù),可以讓curl發(fā)送cookie。
$ curl --cookie "name=xxx" www.example.com
至于具體的cookie的值,可以從http response頭信息的`Set-Cookie`字段中得到。
`-c cookie-file`可以保存服務(wù)器返回的cookie到文件,`-b cookie-file`可以使用這個文件作為cookie信息,進行后續(xù)的請求。
$ curl -c cookies http://example.com
$ curl -b cookies http://example.com
13、增加頭信息
有時需要在http request之中,自行增加一個頭信息。`--header`參數(shù)就可以起到這個作用。
$ curl --header "Content-Type:application/json" http://example.com
14、HTTP認證
有些網(wǎng)域需要HTTP認證,這時curl需要用到`--user`參數(shù)。
$ curl --user name:password example.com
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔相應法律責任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。