22 (:use [clojure.contrib.java-utils :only [as-str]]
33 [clojure.contrib.duck-streams :only [read-lines]]
44 [clojure.contrib.str-utils :only [str-join]])
5- (:import (java.net URL HttpURLConnection)))
5+ (:import (java.net URL HttpURLConnection URLEncoder)
6+ (java.io StringReader)))
67
78(def default-headers {" User-Agent" (str " Clojure/" (clojure-version )
89 " (+http://clojure.org)" ),
910 " Connection" " close" })
1011
12+ (defn url-encode
13+ " Wrapper around java.net.URLEncoder returning a (UTF-8) URL encoded
14+ representation of text."
15+ [text]
16+ (URLEncoder/encode text " UTF-8" ))
17+
1118(defn url
1219 " If u is an instance of java.net.URL then returns it without
1320modification, otherwise tries to instantiate a java.net.URL with
@@ -21,9 +28,10 @@ url as its sole argument."
2128 " Returns a lazy-seq of lines from either the input stream
2229or the error stream of connection, whichever is appropriate."
2330 [connection]
24- (read-lines (if (>= (.getResponseCode connection) 400 )
25- (.getErrorStream connection)
26- (.getInputStream connection))))
31+ (read-lines (or (if (>= (.getResponseCode connection) 400 )
32+ (.getErrorStream connection)
33+ (.getInputStream connection))
34+ (StringReader. " " ))))
2735
2836(defn- parse-headers
2937 " Returns a map of the response headers from connection."
@@ -49,7 +57,7 @@ by a server."
4957(defn- create-cookie-string
5058 " Returns a string suitable for sending to the server in the
5159\" Cookie\" header when given a clojure map of cookies."
52- [cookie-map]
60+ [cookie-map]
5361 (str-join " ; " (map (fn [cookie]
5462 (str (as-str (key cookie))
5563 " ="
@@ -91,6 +99,7 @@ by a server."
9199 {:body-seq (body-seq connection)
92100 :code (.getResponseCode connection)
93101 :msg (.getResponseMessage connection)
102+ :method method
94103 :headers (dissoc headers " Set-Cookie" )
95104 ; ; This correctly implements case-insensitive lookup.
96105 :get-header #(.getHeaderField connection (as-str %))
0 commit comments