乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      curl命令

       憂郁_小剛 2012-11-19
      curl命令 (2012-11-19 19:26)


      http://xshow./blog/1597170

      curl是一種命令行工具,作用是發(fā)出網(wǎng)絡(luò)請求,然后得到和提取數(shù)據(jù),顯示在"標(biāo)準(zhǔn)輸出"(stdout)上面。@舍得Share

      它支持多種協(xié)議,下面舉例講解如何將它用于網(wǎng)站開發(fā)。

      一、查看網(wǎng)頁源碼

      直接在curl命令后加上網(wǎng)址,就可以看到網(wǎng)頁源碼。我們以網(wǎng)址www.sina.com為例(選擇該網(wǎng)址,主要因為它的網(wǎng)頁代碼較短):

        curl www.

        <!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./">here.


      </body></html>

      如果要把這個網(wǎng)頁保存下來,可以使用-o參數(shù),這就相當(dāng)于使用wget命令了。

        curl -o [文件名] www.

      二、自動跳轉(zhuǎn)

      有的網(wǎng)址是自動跳轉(zhuǎn)的。使用-L參數(shù),curl就會跳轉(zhuǎn)到新的網(wǎng)址。

        curl -L www.

      鍵入上面的命令,結(jié)果就自動跳轉(zhuǎn)為www.。

      三、顯示頭信息

      -i參數(shù)可以顯示http response的頭信息,連同網(wǎng)頁代碼一起。

        curl -i www.

        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 >here.


      </body></html>

      -I參數(shù)則是只顯示http response的頭信息。

      四、顯示通信過程

      -v參數(shù)可以顯示一次http通信的整個過程,包括端口連接和http request頭信息。

        curl -v www.

        * 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 >here.


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

      如果你覺得上面的信息還不夠,那么下面的命令可以查看更詳細的通信過程。

        curl --trace output.txt www.

      或者

        curl --trace-ascii output.txt www.

      運行后,請打開output.txt文件查看。

      五、發(fā)送表單信息

      發(fā)送表單信息有GET和POST兩種方法。GET方法相對簡單,只要把數(shù)據(jù)附在網(wǎng)址后面就行。

        curl /form.cgi?data=xxx

      POST方法必須把數(shù)據(jù)和網(wǎng)址分開,curl就要用到--data參數(shù)。

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

      如果你的數(shù)據(jù)沒有經(jīng)過表單編碼,還可以讓curl為你編碼,參數(shù)是--data-urlencode。

        curl --data-urlencode "date=April 1" /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字段,表示你是從哪里跳轉(zhuǎn)過來的。

        curl --referer http://www.

      http://www.

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

      九、cookie

      使用--cookie參數(shù),可以讓curl發(fā)送cookie。

        curl --cookie "name=xxx" www.

      至于具體的cookie的值,可以從http response頭信息的Set-Cookie字段中得到。

      十、增加頭信息

      有時需要在http request之中,自行增加一個頭信息。--header參數(shù)就可以起到這個作用。

        curl --header "xxx: xxxxxx" http://

      十一、HTTP認(rèn)證

      有些網(wǎng)域需要HTTP認(rèn)證,這時curl需要用到--user參數(shù)。

        curl --user name:password 

      【參考資料】

      Using cURL to automate HTTP jobs

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多