我正在尝试基于一个简单的示例(http://www.tothenew.com/blog/using-groovys-http-builder-library/)对本地mongo数据库执行HTTP POST,并且无法保持连接打开足够长的时间来发布任何帖子。这是我的代码:
public static void postToDatabase(String item, final log) {
try {
String baseURL = "http://localhost:27017"
URIBuilder uri = new URIBuilder(baseURL)
HTTPBuilder http = new HTTPBuilder(uri)
String path = "Test/testCollection"
http.post(path: path, body: item, requestContentType: URLENC) { resp ->
log.info("POST Success: ${resp.StatusLine}")
assert resp.StatusLine.statusCode == 201
}
} catch(HttpResponseException ex) {
ex.printStackTrace()
} catch(ConnectException ex) {
ex.printStackTrace()
}
}
一旦程序到达http.post调用,这是我收到的以下堆栈跟踪:
5651 [main] DEBUG groovyx.net.http.HTTPBuilder - POST http://localhost:27017Test/testCollection
5843 [main] DEBUG org.apache.http.impl.conn.BasicClientConnectionManager - Get connection for route {}->http://localhost:27017
5862 [main] DEBUG org.apache.http.impl.conn.DefaultClientConnectionOperator - Connecting to localhost:27017
5890 [main] DEBUG org.apache.http.client.protocol.RequestAddCookies - CookieSpec selected: best-match
6067 [main] DEBUG org.apache.http.client.protocol.RequestAuthCache - Auth cache not set in the context
6067 [main] DEBUG org.apache.http.client.protocol.RequestTargetAuthentication - Target auth state: UNCHALLENGED
6068 [main] DEBUG org.apache.http.client.protocol.RequestProxyAuthentication - Proxy auth state: UNCHALLENGED
6068 [main] DEBUG org.apache.http.impl.client.DefaultHttpClient - Attempt 1 to execute request
6069 [main] DEBUG org.apache.http.impl.conn.DefaultClientConnection - Sending request: POST Test/testCollection HTTP/1.1
6069 [main] DEBUG org.apache.http.wire - >> "POST Test/testCollection HTTP/1.1[\r][\n]"
6071 [main] DEBUG org.apache.http.wire - >> "Accept: */*[\r][\n]"
6071 [main] DEBUG org.apache.http.wire - >> "Content-Length: 122[\r][\n]"
6071 [main] DEBUG org.apache.http.wire - >> "Content-Type: application/x-www-form-urlencoded[\r][\n]"
6071 [main] DEBUG org.apache.http.wire - >> "Host: localhost:27017[\r][\n]"
6071 [main] DEBUG org.apache.http.wire - >> "Connection: Keep-Alive[\r][\n]"
6071 [main] DEBUG org.apache.http.wire - >> "Accept-Encoding: gzip,deflate[\r][\n]"
6072 [main] DEBUG org.apache.http.wire - >> "[\r][\n]"
6072 [main] DEBUG org.apache.http.headers - >> POST Test/testCollection HTTP/1.1
6072 [main] DEBUG org.apache.http.headers - >> Accept: */*
6072 [main] DEBUG org.apache.http.headers - >> Content-Length: 122
6072 [main] DEBUG org.apache.http.headers - >> Content-Type: application/x-www-form-urlencoded
6072 [main] DEBUG org.apache.http.headers - >> Host: localhost:27017
6072 [main] DEBUG org.apache.http.headers - >> Connection: Keep-Alive
6072 [main] DEBUG org.apache.http.headers - >> Accept-Encoding: gzip,deflate
6073 [main] DEBUG org.apache.http.wire - >> "[ { "size" : "10\" - 10\" Double Helix (Class 4) 1 1/4\" Rod" , "strength" : { "unit" : "POUND_FORCE" , "value" : 31000}}]"
6075 [main] DEBUG org.apache.http.impl.conn.DefaultClientConnection - Connection 0.0.0.0:51283<->127.0.0.1:27017 closed
6075 [main] DEBUG org.apache.http.impl.client.DefaultHttpClient - Closing the connection.
6075 [main] DEBUG org.apache.http.impl.conn.DefaultClientConnection - Connection 0.0.0.0:51283<->127.0.0.1:27017 closed
6075 [main] DEBUG org.apache.http.impl.conn.DefaultClientConnection - Connection 0.0.0.0:51283<->127.0.0.1:27017 shut down
6075 [main] DEBUG org.apache.http.impl.conn.BasicClientConnectionManager - Releasing connection org.apache.http.impl.conn.ManagedClientConnectionImpl@50d3bf39
Exception in thread "main" org.apache.http.NoHttpResponseException: The target server failed to respond
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:143)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:57)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:260)
at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:283)
at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:251)
at org.apache.http.impl.conn.ManagedClientConnectionImpl.receiveResponseHeader(ManagedClientConnectionImpl.java:197)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:271)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:123)
at org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:682)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:486)
at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:863)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:476)
at groovyx.net.http.HTTPBuilder.post(HTTPBuilder.java:359)
at groovyx.net.http.HTTPBuilder$post.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:110)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:130)
at com.spidasoftware.engine.api.v1.serializers.MongoLoader.postToDatabase(MongoLoader.groovy:180)
at com.spidasoftware.engine.api.v1.serializers.MongoLoader$postToDatabase$1.callStatic(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:53)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:191)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:219)
at com.spidasoftware.engine.api.v1.serializers.MongoLoader.sortClientItems(MongoLoader.groovy:99)
at com.spidasoftware.engine.api.v1.serializers.MongoLoader$sortClientItems$0.callStatic(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:53)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:191)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:227)
at com.spidasoftware.engine.api.v1.serializers.MongoLoader.parseJSONString(MongoLoader.groovy:76)
at com.spidasoftware.engine.api.v1.serializers.MongoLoader$parseJSONString.callStatic(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:53)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:191)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:219)
at com.spidasoftware.engine.api.v1.serializers.MongoLoader.main(MongoLoader.groovy:45)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
无论出于何种原因,程序似乎都喜欢在它发布之前关闭连接。有什么建议吗?
答案 0 :(得分:1)