Clojure contrib http-agent hangs when making a POST request

瀏覽次數:14 次
跳到第一則未讀訊息

Alex

未讀,
2009年10月29日 晚上7:10:562009/10/29
收件者:Clojure
Hi,

I'm getting some strange errors when trying to make a POST request
using the Clojure contrib http-agent library (http://
richhickey.github.com/clojure-contrib/http.agent-api.html).

When I run:

(use 'clojure.contrib.http.agent)

(println (string (http-agent "http://www.google.com" :method
"POST" :body "param=true")))

The REPL simply hangs forever.

However, when I run the following:

(def agt (http-agent "http://www.google.com" :method "POST" :body
"param=true"))
;...wait a bit or add a (Thread/sleep 1000)
(println (string agt))

I get a correct response (Google saying it doesn't like POST requests)

Also, the (result ... ) function appears to work fine also:

(println (result (http-agent "http://www.google.com" :method
"POST" :body "param=true")))

So it looks like something in the (string ... ) function is causing it
to hang if the response has not yet completed. Anyone have any idea
what might be causing this?

Thanks,

Alex

Does anyone have any ideas why

Rob Wolfe

未讀,
2009年10月30日 下午5:58:282009/10/30
收件者:clo...@googlegroups.com
Alex <alexsp...@gmail.com> writes:

I guess the problem is in "string" function, because it tries
to get "content encoding" of stream which is not available.
In this case output stream was presumably closed by server.
So I can see two solutions:

1) always waiting until request is completed using "result" function

<code>
(ns test2
(:require [clojure.contrib.http.agent :as http]
[clojure.contrib.duck-streams :as ds]))

(let [agnt
(http/http-agent "http://www.google.com"
:method "POST"
:body "param=true")]
(http/result agnt)
(println "string: " (http/string agnt)))
(shutdown-agents)
</code>

2) applying this patch on original clojure.contrib.http.agent,
which imho solves this problem

<patch>
--- a/src/clojure/contrib/http/agent.clj
+++ b/src/clojure/contrib/http/agent.clj
@@ -263,9 +263,12 @@
headers, or clojure.contrib.duck-streams/*default-encoding* if it is
not specified."
([http-agnt]
- (string http-agnt (or (.getContentEncoding
- #^HttpURLConnection (::connection @http-agnt))
- duck/*default-encoding*)))
+ (let [a @http-agnt]
+ (if (= (::state a) ::receiving)
+ (string http-agnt (or (.getContentEncoding
+ #^HttpURLConnection (::connection @http-agnt))
+ duck/*default-encoding*))
+ (string http-agnt duck/*default-encoding*))))
([http-agnt #^String encoding]
(.toString (get-byte-buffer http-agnt) encoding)))
</patch>

HTH,
Rob

Alex

未讀,
2009年10月31日 清晨7:00:562009/10/31
收件者:Clojure
Rob, that's perfect. Thanks very much for looking into that and
supplying the patch. Hopefully we can get that applied to the source
in git.

On Oct 30, 9:58 pm, Rob Wolfe <r...@smsnet.pl> wrote:

Stuart Sierra

未讀,
2009年11月2日 上午11:45:342009/11/2
收件者:Clojure
I've pushed a slightly different fix; (string...) now waits for the
HTTP request to finish.

-SS
回覆所有人
回覆作者
轉寄
0 則新訊息