<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    莊周夢蝶

    生活、程序、未來
       :: 首頁 ::  ::  :: 聚合  :: 管理

    Clojure世界:Http Client

    Posted on 2012-02-13 18:57 dennis 閱讀(6249) 評論(1)  編輯  收藏 所屬分類: Clojure

        使用http client提交表單或者下載網(wǎng)頁也是非常常見的任務(wù),比如使用Java的時候可以用標(biāo)準(zhǔn)庫的HttpURLConnection,也可以選擇Apache Http Client。在clojure里也有這樣的類庫,這里我將介紹三個各有特色的http client實現(xiàn)。

        首先,我最先推薦使用clj-http這個類庫,它是Apache HttpClient的clojure wrapper,是一個提供同步API的簡單易用的Http Client。

    名稱: clj-http
    主頁:https://github.com/dakrone/clj-http
    依賴:
    [clj-http "0.3.1"]

    例子:
    (require '[clj-http.client :as client])
    (client/get "http://google.com")
    結(jié)果:
    => {:cookies {"NID" {:domain ".google.com.hk", :expires #<Date Tue Aug 14 18:20:38 CST 2012>, :path "/", :value "56=qn2OWtODE2D3fUKi_vbi44jZepOeLI9xC4Ta1JQLEicqUvIZAqr7TCmft_hq8i_FRwnFXdTK1jV2S5IrSZFyYhlAN2KcQEXgWX1iK36gM2iYPaKPihuUZDCqgiAamDOl", :version 0}, "PREF" {:domain ".google.com.hk", :expires #<Date Wed Feb 12 18:20:38 CST 2014>, :path "/", :value "ID=8b73a654ff0a2783:FF=0:NW=1:TM=1329128438:LM=1329128438:S=uEM4SsFuHlkqtVhp", :version 0}},
        :status 
    200
        :headers {
    "date" "Sun, 01 Aug 2010 07:03:49 GMT"
                  
    "cache-control" "private, max-age=0"
                  
    "content-type" "text/html; charset=ISO-8859-1"
                  }
        :body 
    "<!doctype html>"
        :trace
    -redirects ["http://google.com" "http://www.google.com/" "http://www.google.fr/"]}
    更多例子:
    (client/get "http://site.com/resources/3" {:accept :json})

    ;; Various options:
    (client
    /post "http://site.com/api"
      {:basic
    -auth ["user" "pass"]
       :body 
    "{\"json\": \"input\"}"
       :headers {
    "X-Api-Version" "2"}
       :content
    -type :json
       :socket
    -timeout 1000
       :conn
    -timeout 1000
       :accept :json})

    ;; Need to contact a server with an untrusted SSL cert
    ?
    (client
    /get "https://alioth.debian.org" {:insecure? true})

    ;; If you don
    't want to follow-redirects automatically:
    (client/get "http://site.come/redirects-somewhere" {:follow-redirects false})

    ;; Only follow a certain number of redirects:
    (client
    /get "http://site.come/redirects-somewhere" {:max-redirects 5})

    ;; Throw an exception 
    if redirected too many times:
    (client
    /get "http://site.come/redirects-somewhere" {:max-redirects 5 :throw-exceptions true})

    ;; Send form params as a urlencoded body
    (client
    /post "http//site.com" {:form-params {:foo "bar"}})

    ;; Multipart form uploads
    /posts
    ;; a map or vector works as the multipart object. Use a vector of
    ;; vectors 
    if you need to preserve order, a map otherwise.
    (client
    /post "http//example.org" {:multipart [["title" "My Awesome Picture"]
                                                  [
    "Content/type" "image/jpeg"]
                                                  [
    "file" (clojure.java.io/file "pic.jpg")]]})
    ;; Multipart values can be one of the following:
    ;; String, InputStream, File, or a 
    byte-array

    ;; Basic authentication
    (client
    /get "http://site.com/protected" {:basic-auth ["user" "pass"]})
    (client
    /get "http://site.com/protected" {:basic-auth "user:pass"})

    ;; Query parameters
    (client
    /get "http://site.com/search" {:query-params {"q" "foo, bar"}})

        clj-http的API相當(dāng)?shù)暮啙嵠粒褂闷饋矸浅1憷瑥?qiáng)烈推薦。題外,學(xué)習(xí)clojure的一個好方法就是為現(xiàn)有的java類庫實現(xiàn)一些方便的clojure wrapper。

        如果你需要異步的http client,我會推薦http.async.client這個類庫,它的API是異步形式的類似 Java的Future模式,對于clojure程序員來說應(yīng)該更像是agent。

    名稱:http.async.client
    主頁:https://github.com/neotyk/http.async.client
    依賴:
    [http.async.client "0.4.1"]
    例子:
    (require '[http.async.client :as c])
    (with-open [client (c/create-client)]
      (let [response (c
    /GET client "http://neotyk.github.com/http.async.client/")]
        (prn (c
    /done? response))
        (c
    /await response)
        (prn (c
    /string response))
        (prn (c
    /status response))
        (prn (c
    /done? response))))

    輸出:
    false
    <!DOCTYPE html 
    {:code 
    200, :msg "OK", :protocol "HTTP/1.1", :major 1, :minor 1}
    true

    更多例子:
    (c/POST client "http://example.com" :body "hello world" :timeout 3000)
    (c
    /DELETE client "http://example.com")
    (c
    /POST client "http://example.com" :body "hello world" :auth {:type :basic :user "admin" :password "admin"})

    請注意,這些方法都是異步調(diào)用的,你需要通過await來等待調(diào)用完成,或者通過done?來判斷調(diào)用是否完成。
    http.async.client有個比較重要的特性就是對Http Chunked編碼的支持,分別通過LazySeq和callback的方式支持,首先看將Http chunked變成一個lazy seq:

    (with-open [client (client/create-client)] ; Create client
      (let [resp (client
    /stream-seq client :get url)]
        (doseq [s (s
    /string resp)]
          (println s))))

    這里非常關(guān)鍵的一點是stream-seq返回的chunk序列,每取一個就少一個(通過first函數(shù)),也就是說每次調(diào)用first取到的chunk都不一樣,是順序遞增,不可重復(fù)獲取的。

    通過callback方式處理:
    (with-open [client (client/create-client)] ; Create client
      (let [parts (ref #{})
            resp (client
    /request-stream client :get url
                                        (fn [state body]
                                          (dosync (alter parts conj (string body)))
                                          [body :
    continue]))]
        ;; 
    do something to @parts
        ))
    自己傳入一個callback函數(shù)接收chunk,比如這里用一個ref累積。

    http.async.client的詳細(xì)文檔看這里:http://neotyk.github.com/http.async.client/docs.html

    最后,有興趣還可以看下aleph這個異步通訊的框架,它支持Http協(xié)議,也提供了http server和client的實現(xiàn)。不過它的API就沒有那么簡單明了,它的模型是類似go語言里利用channel做異步通訊的模型,http只是它的一個模塊罷了,這是另一個話題了。

    轉(zhuǎn)載請注明出處:http://www.tkk7.com/killme2008/archive/2012/02/13/369890.html

    評論

    # re: Clojure世界:Http Client  回復(fù)  更多評論   

    2012-05-19 15:49 by steven.cui
    很棒的類庫,正在嘗試用clojure,嘗試著做些wrapper,的確是快速入門的方式。
    主站蜘蛛池模板: 在线综合亚洲欧洲综合网站| 日日AV拍夜夜添久久免费| 亚洲一级特黄大片无码毛片| 亚洲偷自精品三十六区| 日本h在线精品免费观看| 亚洲人成在线电影| 香蕉免费在线视频| 亚洲精品国产精品乱码视色 | 情侣视频精品免费的国产| 亚洲一日韩欧美中文字幕在线| 麻花传媒剧在线mv免费观看| 亚洲成年人电影在线观看| 国产乱子精品免费视观看片| 中文有码亚洲制服av片| 永久免费观看的毛片的网站| 免费无码专区毛片高潮喷水| 亚洲色偷偷综合亚洲AVYP| 日韩精品在线免费观看| 亚洲欧洲日韩在线电影| 免费的一级黄色片| 日韩a毛片免费观看| 亚洲精品无码久久久久去q| 久久国产精品2020免费m3u8| 91亚洲导航深夜福利| 成年私人影院免费视频网站| 国产成人亚洲综合无| 亚洲成AV人片天堂网无码| 999国内精品永久免费观看| 激情婷婷成人亚洲综合| 中文字幕亚洲乱码熟女一区二区| 免费国产99久久久香蕉| 亚洲香蕉久久一区二区| 亚洲日韩在线第一页| 中国xxxxx高清免费看视频| 亚洲av永久无码精品秋霞电影秋 | 免费一区二区三区四区五区| 中文字幕av免费专区| 亚洲三级视频在线观看| 亚洲国产精品无码久久青草| 69视频在线观看免费| 亚洲aⅴ无码专区在线观看|