http request from Google App Engine

2019-05-24 15:50发布

问题:

I'm trying to make http requests from my Google App Engine webapp, and discovered I have to use URLConnection since it's the only whitelisted class. The corresponding Clojure library is clojure.contrib.http.agent, and my code is as follows:

(defroutes example
  (GET "/" [] (http/string (http/http-agent "http://www.example.com")))
  (route/not-found "Page not found"))

This works fine in my development environment- the browser displays the text for example.com. But when I test it out with Google's development app server:

phrygian:example wei$ dev_appserver.sh war
2010-09-28 14:53:36.120 java[43845:903] [Java CocoaComponent compatibility mode]: Enabled
...
INFO: The server is running at http://localhost:8080/

It just hangs when I load the page. No error, or anything. Any idea what might be going on?

回答1:

http-agent creates threads so that might be why it does not work.

From the API documentation:

Creates (and immediately returns) an Agent representing an HTTP request running in a new thread.

You could try http-connection, which is a wrapper around HttpURLConnection, so this should work.

Another alternative is to try clj-http. The API seems to be a bit more high-level, but it uses Apache HttpComponents which might be blacklisted.

I am guessing http.async.client is a definite no-go due to its strong asynchronous approach.



回答2:

You might want to try appengine.urlfetch/fetch from appengine-clj (http://github.com/r0man/appengine-clj, also in clojars)