java - HTTP请求在eclipse中可以工作,但在编译的JAR包中失败

标签 java apache-httpclient-4.x

我正在使用 Apache HTTPclient 4 将 SPARQL 查询发送到远程存储库。虽然程序相同,但我的 JAR 包得到的响应是 HTTP 406,错误消息是“找不到可接受的文件格式”。在 Eclipse 中,我的查询成功收到 HTTP 200 响应和所需的结果。

什么可能导致具有相同代码的两个环境运行不同?

Eclipse 消息:(HTTP 200)

19:32:23.584 [main] DEBUG o.a.c.h.MultiThreadedHttpConnectionManager - HttpConnectionManager.getConnection:  config = HostConfiguration[host=http://asimov.ludat.lth.se], timeout = 0
19:32:23.585 [main] DEBUG o.a.c.h.MultiThreadedHttpConnectionManager - Allocating new connection, hostConfig=HostConfiguration[host=http://asimov.ludat.lth.se]
19:32:23.585 [main] DEBUG o.a.c.httpclient.HttpConnection - Open connection to asimov.ludat.lth.se:80
19:32:23.589 [main] DEBUG httpclient.wire.header - >> "POST /openrdf-sesame/repositories/KnowRob HTTP/1.1[\r][\n]"
19:32:23.590 [main] DEBUG o.a.c.httpclient.HttpMethodBase - Adding Host request header
19:32:23.593 [main] DEBUG httpclient.wire.header - >> "Content-Type: application/x-www-form-urlencoded; charset=utf-8[\r][\n]"
19:32:23.594 [main] DEBUG httpclient.wire.header - >> "Accept: application/x-binary-rdf-results-table[\r][\n]"
19:32:23.594 [main] DEBUG httpclient.wire.header - >> "Accept: application/sparql-results+xml;q=0.8[\r][\n]"
19:32:23.595 [main] DEBUG httpclient.wire.header - >> "Accept: application/xml;q=0.8[\r][\n]"
19:32:23.596 [main] DEBUG httpclient.wire.header - >> "Accept: text/tab-separated-values;q=0.8[\r][\n]"
19:32:23.596 [main] DEBUG httpclient.wire.header - >> "Accept: text/csv;q=0.8[\r][\n]"
19:32:23.597 [main] DEBUG httpclient.wire.header - >> "User-Agent: Jakarta Commons-HttpClient/3.1[\r][\n]"
19:32:23.597 [main] DEBUG httpclient.wire.header - >> "Host: asimov.ludat.lth.se[\r][\n]"
19:32:23.598 [main] DEBUG httpclient.wire.header - >> "Content-Length: 836[\r][\n]"
19:32:23.598 [main] DEBUG httpclient.wire.header - >> "[\r][\n]"
19:32:23.599 [main] DEBUG httpclient.wire.content - >> "quer[...]rue"
19:32:23.600 [main] DEBUG o.a.c.h.m.EntityEnclosingMethod - Request body sent
19:32:24.304 [main] DEBUG httpclient.wire.header - << "HTTP/1.1 200 OK[\r][\n]"
19:32:24.304 [main] DEBUG httpclient.wire.header - << "HTTP/1.1 200 OK[\r][\n]"
19:32:24.305 [main] DEBUG httpclient.wire.header - << "Server: Apache-Coyote/1.1[\r][\n]"
19:32:24.305 [main] DEBUG httpclient.wire.header - << "Vary: Accept[\r][\n]"
19:32:24.306 [main] DEBUG httpclient.wire.header - << "Content-Disposition: attachment; filename=query-result.brt[\r][\n]"
19:32:24.306 [main] DEBUG httpclient.wire.header - << "Content-Type: application/x-binary-rdf-results-table;charset=ISO-8859-1[\r][\n]"
19:32:24.306 [main] DEBUG httpclient.wire.header - << "Content-Language: en-US[\r][\n]"
19:32:24.307 [main] DEBUG httpclient.wire.header - << "Transfer-Encoding: chunked[\r][\n]"
19:32:24.307 [main] DEBUG httpclient.wire.header - << "Date: Thu, 28 Feb 2013 18:28:24 GMT[\r][\n]"
19:32:24.307 [main] DEBUG httpclient.wire.header - << "[\r][\n]"
19:32:24.308 [main] DEBUG org.openrdf.http.client.HTTPClient - reponse MIME type is application/x-binary-rdf-results-table

JAR 消息:(HTTP 406)

19:15:35.178 [main] DEBUG o.a.c.h.MultiThreadedHttpConnectionManager - HttpConnectionManager.getConnection:  config = HostConfiguration[host=http://asimov.ludat.lth.se], timeout = 0
19:15:35.179 [main] DEBUG o.a.c.h.MultiThreadedHttpConnectionManager - Allocating new connection, hostConfig=HostConfiguration[host=http://asimov.ludat.lth.se]
19:15:35.179 [main] DEBUG o.a.c.httpclient.HttpConnection - Open connection to asimov.ludat.lth.se:80
19:15:35.183 [main] DEBUG httpclient.wire.header - >> "POST /openrdf-sesame/repositories/KnowRob HTTP/1.1[\r][\n]"
19:15:35.184 [main] DEBUG o.a.c.httpclient.HttpMethodBase - Adding Host request header
19:15:35.187 [main] DEBUG httpclient.wire.header - >> "Content-Type: application/x-www-form-urlencoded; charset=utf-8[\r][\n]"
19:15:35.188 [main] DEBUG httpclient.wire.header - >> "Accept: text/tab-separated-values;q=0.8[\r][\n]"
19:15:35.188 [main] DEBUG httpclient.wire.header - >> "Accept: text/csv;q=0.8[\r][\n]"
19:15:35.189 [main] DEBUG httpclient.wire.header - >> "User-Agent: Jakarta Commons-HttpClient/3.1[\r][\n]"
19:15:35.189 [main] DEBUG httpclient.wire.header - >> "Host: asimov.ludat.lth.se[\r][\n]"
19:15:35.189 [main] DEBUG httpclient.wire.header - >> "Content-Length: 836[\r][\n]"
19:15:35.190 [main] DEBUG httpclient.wire.header - >> "[\r][\n]"
19:15:35.191 [main] DEBUG httpclient.wire.content - >> "query[...]er=true"
19:15:35.191 [main] DEBUG o.a.c.h.m.EntityEnclosingMethod - Request body sent
19:15:35.717 [main] DEBUG httpclient.wire.header - << "HTTP/1.1 406 Not Acceptable[\r][\n]"
19:15:35.717 [main] DEBUG httpclient.wire.header - << "HTTP/1.1 406 Not Acceptable[\r][\n]"
19:15:35.718 [main] DEBUG httpclient.wire.header - << "Server: Apache-Coyote/1.1[\r][\n]"
19:15:35.718 [main] DEBUG httpclient.wire.header - << "Vary: Accept[\r][\n]"
19:15:35.718 [main] DEBUG httpclient.wire.header - << "Content-Type: text/plain;charset=UTF-8[\r][\n]"
19:15:35.719 [main] DEBUG httpclient.wire.header - << "Content-Language: en-US[\r][\n]"
19:15:35.719 [main] DEBUG httpclient.wire.header - << "Content-Length: 32[\r][\n]"
19:15:35.719 [main] DEBUG httpclient.wire.header - << "Date: Thu, 28 Feb 2013 18:11:35 GMT[\r][\n]"
19:15:35.720 [main] DEBUG httpclient.wire.header - << "[\r][\n]"
19:15:35.720 [main] DEBUG o.a.c.httpclient.HttpMethodBase - Buffering response body
19:15:35.720 [main] DEBUG httpclient.wire.content - << "No acceptable file format found."
19:15:35.721 [main] DEBUG o.a.c.httpclient.HttpMethodBase - Resorting to protocol version default close connection policy
19:15:35.721 [main] DEBUG o.a.c.httpclient.HttpMethodBase - Should NOT close connection, using HTTP/1.1
19:15:35.721 [main] DEBUG o.a.c.httpclient.HttpConnection - Releasing connection back to connection manager.
19:15:35.721 [main] DEBUG o.a.c.h.MultiThreadedHttpConnectionManager - Freeing connection, hostConfig=HostConfiguration[host=http://asimov.ludat.lth.se]
19:15:35.722 [main] DEBUG o.a.c.h.util.IdleConnectionHandler - Adding connection at: 1362075335721
19:15:35.722 [main] DEBUG o.a.c.h.MultiThreadedHttpConnectionManager - Notifying no-one, there are no waiting threads
19:15:35.722 [main] WARN  org.openrdf.http.client.HTTPClient - Server reports problem: No acceptable file format found.

最佳答案

在第一种情况下,您的应用程序/环境将 header 发送为

19:32:23.594 [main] DEBUG httpclient.wire.header - >> 
"Accept: application/x-binary-rdf-results-table[\r][\n]"

但在第二种情况下缺少此内容,因此您的第二个环境无法接受 'application/x-binary-rdf-results-table' 类型的响应。您在哪里运行第二个查询? jar部署在哪里?您可能需要将此编码/类型添加到您的应用服务器。

我不太清楚您在哪里使用它,但好的起点是了解为什么在第二种情况下缺少“Accept:application/x-binary-rdf-results-table”。

关于java - HTTP请求在eclipse中可以工作,但在编译的JAR包中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15143043/

相关文章:

java - DocumentBuilder 解析 xml 错误

java - SQL查询代码背后的逻辑来获取数据?

java - 如何正确处理HttpClientException

java - 如何在 Android 上使用外部库使用的 Apache http-client 4.5.x

java - 如何在 java 中设置 SSL 协议(protocol)版本?我怎么知道是哪一个? javax.net.ssl.SSLException : Received fatal alert: protocol_version 异常

java - 哪个是在 java 中声明记录器变量的最佳方式

java - getStringArrayExtra() 返回 null

java - 抓取网页编码问题-以字节为单位的负值

httpclient - 在 EJB 容器中使用 PoolingHttpClientConnectionManager

java - 我如何指定一个对象在 Java 中实现一个接口(interface)?